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\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
]);