PHP code example of tandrezone / zroute

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

    

tandrezone / zroute example snippets




Route\Router;

$router = new Router();

// Static route
$router->get('/', fn($p) => print "Home page\n");

// Dynamic route — parameter syntax: $paramName or {paramName}
$router->get('/products/$product-slug', function (array $params) {
    echo "Product: " . htmlspecialchars($params['product-slug']) . "\n";
});

// 404 handler
$router->notFound(function (string $path) {
    http_response_code(404);
    echo "404 – Not found: " . htmlspecialchars($path) . "\n";
});

// Dispatch the current HTTP request
$router->run();

[
    'name'       => 'resource.show',          // optional logical name
    'method'     => 'GET',                    // HTTP verb
    'path'       => '/resources/{id}',        // URI pattern
    'callback'   => [MyController::class, 'show'],
    'middleware' => [
        MyAuthMiddleware::class,
        MyCsrfMiddleware::class,
    ],
    'parameters' => [
        'id' => [
            'type'   => 'integer',
            'source' => 'path',   // 'path' | 'query' | 'body' | 'auto'
            'regex'  => '[0-9]+', // optional regex constraint (path params only)
        ],
        'page' => [
            'type'     => 'integer',
            '

$router->loadFromArray($definitions);

// routes.php
return [
    ['name' => 'home', 'method' => 'GET', 'path' => '/', 'callback' => [HomeController::class, 'index'], ...],
];

// bootstrap.php
$router->loadFromFile(__DIR__ . '/routes.php');

$router->loadFromFile(__DIR__ . '/routes.php', [
    'package_config_namespace' => [
        'redirect_target_route' => '/dashboard',
    ],
]);

use zRoute\Contracts\MiddlewareInterface;
use zRoute\Request;

class AuthenticateUser implements MiddlewareInterface
{
    public function handle(Request $request, callable $next): mixed
    {
        if (!$request->getHeader('Authorization')) {
            throw new \RuntimeException('Unauthorized', 401);
        }
        return $next($request);
    }
}

public function show(Request $request): mixed
{
    $id      = $request->getParam('id');        // path > query > body
    $page    = $request->getParam('page', 1);   // with default
    $all     = $request->all();                 // merged array
    $accept  = $request->getHeader('Accept');
    $method  = $request->getMethod();

    // …
}

$router->get(string $pattern, callable $handler): static
$router->post(string $pattern, callable $handler): static
$router->put(string $pattern, callable $handler): static
$router->patch(string $pattern, callable $handler): static
$router->delete(string $pattern, callable $handler): static
$router->any(string $pattern, callable $handler): static          // all common methods
$router->addRoute(string $method, string $pattern, callable $handler): static

$router->loadFromArray(array $definitions, array $config = []): static
$router->loadFromFile(string $filePath, array $config = []): static

$router->getNamedRoute(string $name): ?Route

$router->notFound(function (string $path): void { ... });
$router->methodNotAllowed(function (string $method, string $path): void { ... });

$router->run(): mixed          // dispatch from $_SERVER
$router->dispatch(string $method, string $path): mixed
nginx
location / {
    try_files $uri $uri/ /index.php?$query_string;
}