PHP code example of mycodebox / minirouter

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

    

mycodebox / minirouter example snippets




use MyCodebox\MiniRouter\Core\MiniRouter;

$router = new MiniRouter();
$router->get('/hello/{name}', function ($req, $res, $args) {
    return $res->withHeader('Content-Type', 'text/plain')
                ->withBody('Hello, ' . $args['name'] . '!');
});
$router->dispatch();

$router->addMiddleware(function ($req, $res, $next) {
    // Logging, authentication, etc.
    return $next($req, $res);
});

$router->get('/secure', $handler)
       ->addMiddleware(function ($req, $res, $next) {
           // Route-specific logic
           return $next($req, $res);
       });

$router->group('/api', function ($group) {
    $group->addMiddleware(function ($req, $res, $next) {
        // Group-specific logic
        return $next($req, $res);
    });
    $group->get('/user/{id}', $handler);
});

class ApiKeyMiddleware implements MiniMiddlewareInterface {
    public function process($req, $res, $next) {
        // Class-based middleware logic
        return $next($req, $res);
    }
}
$router->addMiddleware(new ApiKeyMiddleware());

use MyCodebox\MiniRouter\Core\MiniContainer;
$container = new MiniContainer();
$container->set('greetingService', fn() => fn($name) => "Hello, $name!");
$router = new MiniRouter($container);
$router->get('/hello/{name}', function ($req, $res, $args) use ($container) {
    $greeting = $container->get('greetingService');
    return $res->withBody($greeting($args['name']));
});

$container->set('greetingService', fn($c) => fn($name) => "Hello, $name!");
$greet = $container->get('greetingService');
echo $greet('Max'); // Hello, Max!

$container->set('simpleGreeting', fn() => fn($name) => "Hi, $name!");
$greet = $container->get('simpleGreeting');
echo $greet('Anna'); // Hi, Anna!

class GreetingService {
    public function greet($name) {
        return "Hello, $name!";
    }
}
$container->set('greetingService', fn($c) => new GreetingService());
$service = $container->get('greetingService');
echo $service->greet('Tom'); // Hello, Tom!

$container->set('config', fn() => [
    'greeting' => 'Hello',
    'farewell' => 'Goodbye'
]);
$config = $container->get('config');
echo $config['greeting'] . ', Max!'; // Hello, Max!

$container->set('greetingService', function($c) {
    return function($name) {
        return "Hello, $name!";
    };
});
$greet = $container->get('greetingService');
echo $greet('Lisa'); // Hello, Lisa!

  // Route that responds to multiple methods:
  $router->any(['GET', 'POST', 'put'], '/any-demo', function ($req, $res) {
      return $res->withBody(['method' => $req->method]);
  });
  // Methods can be written in any case!
  

$router->get('/product/{id:\d+}', function ($req, $res, $args) {
    // $args['id'] is guaranteed to be a number
});

$router->get('/blog/{year:\d{4}}/{slug}', function ($req, $res, $args) {
    // $args['year'] = "2023", $args['slug'] = "my-article"
});

$router->get('/foo/{bar}', function ($req, $res, $args) {
    // $args['bar'] accepts anything except slash
});

// ANY route example:
$router->any(['get', 'POST'], '/any', function ($req, $res) {
    return $res->withBody(['method' => $req->method]);
});
// Methods can be written in any case!

$router->group('/api', function ($group) {
    $group->addMiddleware(new ApiKeyMiddleware());
    $group->get('/user/{id:\d+}', $handler);
    $group->post('/user', $handler);
    // ...
});

$router->get('/hello/{name}', $handler)->setName('hello_route');
$url = $router->urlFor('hello_route', ['name' => 'World']); // /hello/World

try {
    $router->dispatch();
} catch (Throwable $e) {
    MiniUtils::errorResponse($e, true)->send(); // true = debug mode
}
bash
php -S localhost:8080 ./example/demo.php