PHP code example of rudra / router

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

    

rudra / router example snippets


use Rudra\Router\Router;
use Rudra\Container\Rudra;

$router = new Router(Rudra::run());

use Rudra\Container\Facades\Rudra;  
use Rudra\Router\RouterFacade as Router;
use Rudra\Container\Interfaces\RudraInterface;

Rudra::binding()->set([RudraInterface::class => Rudra::run()]);

$router->get('hello/:name', function ($name) {
    echo "Hello $name!";
});

$router->get('user/:[\d]{1,3}', function ($id) {
    echo "User ID: $id";
});

$router->get('read/:id', [MainController::class, 'read']);

Router::get('callback/:name', function ($name) {
    echo "Hello $name!";
});

Router::get('read/:id', [MainController::class, 'read']);

$router->get('read/:id',      [MainController::class, 'read']);
$router->post('create/:id',   [MainController::class, 'create']);
$router->put('update/:id',    [MainController::class, 'update']);
$router->patch('patch/:id',   [MainController::class, 'patch']);
$router->delete('delete/:id', [MainController::class, 'delete']);

$router->any('any/:id', [MainController::class, 'any']);

$router->get('read/page', [MainController::class, 'read'], [
    'before' => [AuthMiddleware::class],
    'after'  => [LogMiddleware::class]
]);

$router->get('admin/:id', [AdminController::class, 'show'], [
    'before' => [
        AuthMiddleware::class,
        [RoleMiddleware::class, ['role' => 'admin', new PermissionChecker()]]
    ],
    'after' => [
        LogMiddleware::class,
        [CacheMiddleware::class, ['ttl' => 3600]]
    ]
]);

use Rudra\Router\RouterFacade as Router;

class AuthMiddleware
{
    public function __invoke($next, ...$params)
    {
        // Logic before controller
        if (!Auth::check()) {
            throw new UnauthorizedException();
        }

        // Pass control to the next middleware in chain
        if ($next) {
            Router::handleMiddleware($next);
        }

        // Logic after controller (optional)
    }
}

class UnsetSessionMiddleware
{
    public function __invoke($next, ...$params)
    {
        Session::remove('value');
        Session::remove('alert');
        Session::remove('errors');

        if ($next) {
            Router::handleMiddleware($next);
        }
    }
}

$router->resource('api/users', 'api/user', UserController::class);

$router->resource('api/posts', 'api/post', PostController::class, [
    'actionIndex',   // GET    api/posts       — list all posts
    'actionView',    // GET    api/post/:id    — get single post
    'actionAdd',     // POST   api/posts       — create new post
    'actionUpdate',  // PUT/PATCH api/post/:id — update post
    'actionDrop'     // DELETE api/post/:id    — delete post
]);

$router->set([
    'url'        => '/api/users/:id',
    'method'     => 'GET|POST',
    'controller' => [UserController::class, 'handle'],
    'middleware' => [
        'before' => [AuthMiddleware::class],
        'after'  => [LogMiddleware::class]
    ]
]);

class UserController extends Controller
{
    public function show(int $id, Request $request, UserService $service)
    {
        // $id — from URL
        // $request and $service — injected automatically
    }
}