PHP code example of crell / carica

1. Go to this page and download the library: Download crell/carica library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

crell / carica example snippets


// More on this in a moment.
$router = make_a_router();

$app = new \Crell\Carica\StandardApplication(
responseFactory: $aPSR17ResponseFactory,
streamFactory: $aPsr17StreamFactory,
router: $router,
);

use Crell\Carica\ParsedBody;
use Crell\Carica\RequesteAttribute;

class SomeAction
{
    public function __construct(
        private ServiceA $serviceA,
        private ServiceB $serviceB,
    ) {}
    
    public __invoke(
        #[ParsedBody] 
        Message $body, 
        Bar $bar,
        string $qix,
        ServerRequestInterface $request,
        #[RequestAttribute('some_known_key')]
        string $someValue,
        string $narf = 'default', 
    ): array {
        // Do some logic, using ServiceA and ServiceB.
        
        return ['result' => 'data'];
    }
}

use Crell\Carica\RequesteAttribute;
use Crell\Carica\Middleware;

class SomeAction
{
    public function __construct(
        private ServiceA $serviceA,
        private ServiceB $serviceB,
    ) {}
    
    #[Middleware(SomeMiddleware::class), Middleware('service_id')]
    public __invoke(int $foo, string $bar): array 
    {
        // Do some logic, using ServiceA and ServiceB.
        
        return ['result' => 'data'];
    }
}

use Crell\Carica\Router\FastRoute\PreParsingRouteCollector;
use FastRoute\Dispatcher\GroupCountBased as RouteDispatcher;

function fastRouteDispatcherFactory(bool $useCache, string $cacheFile, \Closure $callback): RouteDispatcher
{
// Load cached router if available.
if ($useCache && file_exists($cacheFile)) {
  $dispatchData = vMethod)) {
  self::$envMethod($collector);
}

$dispatchData = $collector->getData();

if ($useCache) {
  file_put_contents(self::CacheFile, ' return ' . var_export($dispatchData, true) . ';');
}

return new RouteDispatcher($dispatchData);
}

fastRouteDispatcherFactory(true, 'cache/routing.cache', function (PreParsingRouteCollector $r) {
    $r->addRoute('GET', '/user/{name}/{id:[0-9]+}', [SomeClass::class, 'aMethod']);
    $r->get('/user/{id:[0-9]+}', AnInvokableClass::class), ['extra' => 'stuff'];
})