PHP code example of chevere / router

1. Go to this page and download the library: Download chevere/router 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/ */

    

chevere / router example snippets


// Define routes
$routes = routes(
    route('/hello', GET: 'hello.twig'),
    route('/api/users',
        GET: UserListController::class,
        POST: headless(UserCreateController::class, CsrfMiddleware::class)
    ),
    route('/products/{id}',
        GET: bind('product.twig', ProductGetController::class),
        PUT: ProductUpdateController::class,
        DELETE: ProductDeleteController::class
    )
);
// Create router
$router = router($routes);
// Handle request
$routed = $router->routed($serverRequest, $responseFactory, $container);
$response = $routed->response();
$return = $routed->return(); // Controller return value

$route = route(
    '/product/{id}',
    GET: bind('product.twig', ProductGet::class),
    DELETE: ProductDelete::class,
);

class ProductGet extends Controller
{
    public function __invoke(string $id): array
    {
        // ...
        return $context;
    }
}

class ProductDelete extends Controller
{
    public function __invoke(string $id): void
    {
        // ...
    }
}

$bind = bind(
    view: 'product.twig',
    controller: ProductGet::class,
    ...$middleware // PSR-15
)

$bind->controllerName(); // ProductGet

$bind->view(); // product.twig

$bind->middlewares();

headless(ProductGet::class, ...$middleware)

route('/hello-world', GET: ..., POST: ...)

route('/', GET: 'home.twig')

route(
    '/product/{id}',
    DELETE: ProductDelete::class,
)

route('/products/{id}', GET: MyController::class);

public function __invoke(string $id) {...}

use Chevere\Parameter\Attributes\_string;

public function __invoke(
    #[_string('/\d+/')]
    string $id
) {
    // $id is digits only
}

route(
    '/login',
    view: 'login.twig',
    GET: LoginGet::class,
    POST: LoginPost::class,
)

route(
    '/signup',
    view: 'signup.twig',
    middleware: middlewares(
        RedirectIfLogged::class, // PSR-15
        TurnstileVerify::class, // PSR-15
    ),
    GET: SignUpGet::class,
    POST: SignUpPost::class,
)

route(
    '/logout',
    exclude: middlewares(
        TurnstileVerify::class, // PSR-15
        ChallengeTwoFactor::class, // PSR-15
    ),
    POST: LogoutPost::class,
)

$middlewares = middlewares(
    SessionMiddleware::class,
    AuthMiddleware::class
);

$routes = routes(
    route(...),
    routes(...),
);

$routes = $routes->withRoute($route);
$routes = $routes->withRoutes($moreRoutes);

$routes = $routes->has('/pricing'); // bool
$home = $routes->get('/pricing'); // RouteInterface

$routes = $routes->withPrependMiddleware(
    middlewares(
        SessionSetUpCSRFToken::class, // PSR-15
    )
)

$routes = $routes->withAppendMiddleware(
    middlewares(
        SessionCheckCSRFToken::class, // PSR-15
    )
)

$router->views()->assert($viewsDir);

$container = new Container(
    database: $database,
    // other entries
);

$container = $container->with(
    status: 'challenged',
    challenge: '2fa',
);

$container->has('session'); // bool true
$session = $container->get('session'); // SessionInterface

$container = $container->withAutoInject($deps, ...$ignore);

$deps = $router->dependencies();

$deps->assert($container);

$routed = $router->routed(
    $serverRequest,   // PSR-7
    $responseFactory, // PSR-17
    $container,       // PSR-11
    $callback
);

use Chevere\Router\Interfaces\ContainerInterface;

$callback = function (ContainerInterface $container): ContainerInterface {
    $session = $container->get('sessionFactory')->newSession(
        $container->get('requestUser')->sessionId
    );

    return $container->with(
        session: $session,
        user: $session->getOrDefault('user')
    );
};

$hasThrowable = $routed->hasThrowable(); // bool
$throwable = $routed->throwable();

if ($routed->hasThrowable()
    && ! ($routed->throwable() instanceof ControllerException)
) {
    throw $routed->throwable();
}

$psr7Response = $routed->response();

$bind = $routed->bind();

$controllerReturn = $routed->return();