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";
});
use Rudra\Router\Attributes\Routing;
class UserController
{
#[Routing(url: 'users/:id')]
public function show(int $id): void
{
// handle /users/123
}
}
#[Routing(url: 'admin[/item[/page[/:page]]]')]
public function adminPanel(): void {}
#[Routing(url: ['blog', 'articles', 'news'])]
public function list(): void {}
#[Routing(url: ['admin[/item[/page[/:page]]]', 'manager[/item[/page[/:page]]]'])]
public function adminPanel(): void {}
#[Routing(url: 'lang/:[a-z]{1,3}')] // /lang/en, /lang/ru
public function setLanguage(): void {}
#[Routing(url: 'page/:[\d]{1,3}')] // /page/1 ... /page/999
public function showPage(): void {}
#[Routing(url: 'admin[/item]/:[a-z]+')] // combination: optional + regex
public function mixedRoute(): void {}
use Rudra\Router\Attributes\Routing;
use Rudra\Router\Attributes\Middleware;
#[Routing(url: 'admin/:id', method: 'GET')]
#[Middleware(name: "Auth", params: ["admin"])]
public function show(int $id): void
{
// Only accessible for authenticated admins
}
#[Routing(url: 'admin[/item[/page]]', method: 'POST')]
public function adminAction(): void {}
// All 3 expanded routes will be registered with method: POST
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/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
]);