PHP code example of bermudaphp / router

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

    

bermudaphp / router example snippets


 use Bermuda\Router\Routes;

 $routes = new Routes;
 $router = Router::fromDnf($routes);

 $routes->addRoute(
     RouteRecord::get('home', '/hello/[name]', static function(string $name): void {
         echo sprintf('Hello, %s!', $name)
     })
 ); 
 
 $route = $router->match($_SERVER['REQUEST_URI'], $_SERVER['REQUEST_METHOD']);
 if (!route) {
     // route not found logics
 }
 
 call_user_func($route->handler, $route->params['name']);
 

 echo $router->generate('home', ['name' => 'Jane Doe']); // Output /hello/Jane%20Doe
 

 
 $pipeline = new \Bermuda\Pipeline\Pipeline();
 $factory = new \Bermuda\MiddlewareFactory\MiddlewareFactory($container, $responseFactory);
 
 class Handler implements RequestHandlerInterface
 {
    public function handle(ServerRequestInterface $request): ResponseInterface
    {
        return new TextResponse(sprintf('Hello, %s!', $request->getAttribute('name')))
    }
 };
 
 $routes->addRoute(
     RouteRecord::get('home', '/hello/[name:[a-z]]', Handler::class)
 ); 
 
 $pipeline->pipe($factory->make(Bermuda\Router\Middleware\MatchRouteMiddleware::class));
 $pipeline->pipe($factory->make(Bermuda\Router\Middleware\DispatchRouteMiddleware::class)
     ->setFallbackHandler($container->get(Bermuda\Router\Middleware\RouteNotFoundHandler::class)));
  
 $response = $pipeline->handle($request);

 send($response)
 

 class Handler implements RequestHandlerInterface
 {
    public function handle(ServerRequestInterface $request): ResponseInterface
    {
        $route = $request->getAttribute('Bermuda\Router\Middleware\RouteMiddleware')->route; // MatchedRoute instance
    }
 }; 
 

 RouteRecord::get(string $name, string $path, mixed $handler): RouteRecord ;
 RouteRecord::post(string $name, string $path, mixed $handler): RouteRecord ;
 RouteRecord::patch(string $name, string $path, mixed $handler): RouteRecord ;
 RouteRecord::put(string $name, string $path, mixed $handler): RouteRecord ;
 RouteRecord::delete(string $name, string $path, mixed $handler): RouteRecord ;
 RouteRecord::options(string $name, string $path, mixed $handler): RouteRecord ;
 RouteRecord::head(string $name, string $path, mixed $handler): RouteRecord ;
 

 $routes->addRoute(RouteRecord::get('users.get, '/api/v1/users/[id:[a-zA-Z]]', static function(ServerRequestInterface $request): ResponseInterface {
     return findUser($request->getAttribute('id'));
 }));

 alternative:
 $routes->addRoute(RouteRecord::get('users.get, '/api/v1/users/[id]', static function(ServerRequestInterface $request): ResponseInterface {
     return findUserById($request->getAttribute('id'));
 })->withToken('id', '[a-zA-Z]'));
 

 $routes->addRoute(RouteRecord::get('users.get, '/api/v1/users/[?id]', static function(ServerRequestInterface $request): ResponseInterface {
     if (($id = $request->getAttribute('id')) !== null) {
         return findUserById($id);
     }
     
     return get_all_users();
 }));
 

 $group = $routes->group(name: 'api', prifix: '/api'); // set routes group

$group->addRoute(RouteRecord::get('users.get, 'users/[?id]', GetUserHandler::class));
 $group->addRoute(RouteRecord::post(user.create, 'users', CreateUserHandler::class));

 $group->setMiddleware([GuardMiddleware::class]) // set middleware for all routes in group
 $group->setTokens(['id' => '[a-zA-Z]']) // set tokens for all routes in group


 $group = $routes->group('api') // get routes group from name
 
 

 
 $routes->cache('path/to/cached/routes/file.php');
 $routes = Routes::createFromCache('path/to/cached/routes/file.php')
 
 $router = Router::fromDnf($routes);
 

 $app = new App;
 $repository = new UserRepository;
 $routes->addRoute(RouteRecord::get('user.get', '/users/[id]', static function(ServerRequest $request) use ($app, $repository): ResponseInterface {
    return $app->respond(200, $repository->findById($request->getAttribute('id')));
 }));

 $routes->cache('path/to/cached/routes/file.php');
 $routes = Routes::createFromCache('path/to/cached/routes/file.php', compact('app', 'repository'));