PHP code example of monkeyscloud / monkeyslegion-router

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

    

monkeyscloud / monkeyslegion-router example snippets



declare(strict_types=1);

use MonkeysLegion\Router\Router;
use MonkeysLegion\Router\RouteCollection;

$router = new Router(new RouteCollection());

$router->get('/users', fn($req) => new Response(
    Stream::createFromString(json_encode(['users' => []])),
    200,
    ['Content-Type' => 'application/json'],
), 'users.index');

$router->get('/users/{id:\d+}', fn($req, $id) => new Response(
    Stream::createFromString(json_encode(['id' => $id])),
    200,
), 'users.show');

$response = $router->dispatch($request);

#[RoutePrefix('/api/v2/users')]
#[Middleware(['cors', 'throttle:60,1'])]
final class UserController
{
    #[Route('GET', '/', name: 'users.index', summary: 'List users', tags: ['Users'])]
    public function index(ServerRequestInterface $request): Response
    {
        return UserResource::collection($this->users->paginate())->toResponse();
    }

    #[Route('GET', '/{id:\d+}', name: 'users.show')]
    public function show(ServerRequestInterface $request, string $id): Response
    {
        return UserResource::make($this->users->findOrFail((int) $id))->toResponse();
    }

    #[Route('POST', '/', name: 'users.store')]
    #[Throttle(max: 10, per: 60)]
    public function store(CreateUserRequest $dto): Response
    {
        return UserResource::make($this->users->create($dto))->toResponse(status: 201);
    }
}

#[ApiResource(prefix: '/photos', parameter: 'photo', only: ['index', 'show', 'store', 'update', 'destroy'])]
final class PhotoController
{
    public function index(ServerRequestInterface $req): Response { /* ... */ }
    public function show(ServerRequestInterface $req, string $photo): Response { /* ... */ }
    public function store(ServerRequestInterface $req): Response { /* ... */ }
    public function update(ServerRequestInterface $req, string $photo): Response { /* ... */ }
    public function destroy(ServerRequestInterface $req, string $photo): Response { /* ... */ }
}

$scanner = new ControllerScanner($router);
$scanner->scan(__DIR__ . '/app/Controller', 'App\\Controller');

$router->group()
    ->prefix('/api/v2')
    ->middleware('auth')
    ->domain('api.example.com')
    ->group(function (Router $r) {
        $r->get('/users', $handler, 'api.users.index');
        $r->post('/users', $handler, 'api.users.store');
    });

// Register named middleware
$router->registerMiddleware('auth', new AuthMiddleware(), priority: 100);
$router->registerMiddleware('cors', new CorsMiddleware(), priority: 200);

// Middleware groups
$router->registerMiddlewareGroup('api', ['cors', 'auth']);

// Global middleware (runs on every request)
$router->addGlobalMiddleware('timing');

// Per-method exclusion (unique to MonkeysLegion):
#[Middleware('auth')]
class AdminController
{
    #[Route('GET', '/login')]
    #[WithoutMiddleware('auth')]  // Login doesn't need auth
    public function login(): Response { /* ... */ }
}

$router->url('users.show', ['id' => 42]);
// → /users/42

// Model binding
$router->url('users.show', ['id' => $userEntity]);
// → /users/{entity->id}

// Absolute URLs
$router->getUrlGenerator()->baseUrl = 'https://api.example.com';
$router->url('users.index', absolute: true);
// → https://api.example.com/users

$signed = new SignedUrlGenerator($router->getUrlGenerator(), $secret);

$url = $signed->generate('verify-email', ['id' => 42], expiration: 3600);
$signed->validate($url); // true

$url = $signed->temporarySignedRoute('download', 300, ['file' => 'report.pdf']);

$router->get('/users/{id:int}', $handler);
$router->get('/posts/{uuid:uuid}', $handler);
$router->get('/articles/{slug:slug}', $handler);
$router->get('/events/{date:date}', $handler);
$router->get('/records/{ulid:ulid}', $handler);

$cache = new RouteCache('/var/cache/routes');

if ($cache->has() && !$cache->isStale($sourceFiles)) {
    $compiled = $cache->load();
    $router->loadCompiled($compiled);
} else {
    // Register routes...
    $router->compile();
    $cache->save($router->getCompiledRoutes());
}

$debugger = new RouteDebugger($router);

// ASCII table output
echo $debugger->render();

// Test a specific request (like Symfony's router:match)
$result = $debugger->match('GET', '/users/42');
// ['matched' => true, 'route' => [...], 'params' => ['id' => '42']]

// Filter routes
$getRoutes = $debugger->filter(method: 'GET');
$apiRoutes = $debugger->filter(pathContains: '/api');

#[Route('POST', '/login')]
#[Throttle(max: 5, per: 300, by: 'ip')]  // 5 attempts per 5 minutes per IP
public function login(): Response { /* ... */ }