PHP code example of maduser / argon-routing

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

    

maduser / argon-routing example snippets


use Maduser\Argon\Container\ArgonContainer;
use Maduser\Argon\Routing\Router;
use Maduser\Argon\Routing\RouteManager;
use Maduser\Argon\Routing\RouteMatcher;
use App\Http\Middleware\AuthMiddleware;
use App\Http\Middleware\CsrfMiddleware;
use Nyholm\Psr7\Factory\Psr17Factory;

$container = new ArgonContainer();
$routes = new RouteManager();                // defaults to the in-memory store
$router = new Router($container, $routes);

// Define routes just like in a typical HTTP kernel
$router->get('/users/{id}', 'UsersController@show', middleware: [AuthMiddleware::class]);
$router->post('/users', 'UsersController@store', middleware: [AuthMiddleware::class, CsrfMiddleware::class]);

// Match an incoming request
$psr17 = new Psr17Factory();
$request = $psr17->createServerRequest('GET', '/users/42');

$matcher = new RouteMatcher($routes);
$matched = $matcher->match($request);

// Inspect the matched route (all PSR types)
$matched->getHandler();      // UsersController@show
$matched->getArguments();    // ['id' => '42']
$matched->getMiddlewares();  // [AuthMiddleware::class]

$router->get('/health', HealthController::class);
$router->get('/users/{id}', [UsersController::class, 'show']);
$router->get('/articles/{slug}', ArticlesController::class . '@show');

final class UsersController
{
    public function show(string $id): array
    {
        return ['id' => $id];
    }
}

// Register the interceptors that should run before your handlers are invoked.
// Each can examine the matched route from the request attribute, inspect payloads, etc.
$container->registerInterceptor(EntityBindingInterceptor::class);
$container->registerInterceptor(JsonPayloadInterceptor::class);
$container->registerInterceptor(RequestValidationInterceptor::class);

$router->get('/users/{user}', [UsersController::class, 'show']);

final class UsersController
{
    public function show(User $userDto): ResponseInterface
    {
        // $userDto already passed through the interceptors
    }
}