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


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

use Rudra\Router\RouterFacade as Router;

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

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

/**
 * @Routing(url = ''read/:id')
 */
public function read($id)

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

/**
 * @Routing(url = 'read/page')
 * @Middleware(name = 'App\Middleware\Middleware')
 */
public function read()

$router->annotationCollector([
    \App\Controllers\MainController::class,
    \App\Controllers\SecondController::class,
]);

Router::annotationCollector([
    \App\Controllers\MainController::class,
    \App\Controllers\SecondController::class,
]);

$router->get('', [MainController::class, 'read'], [
    'before' => [FirstMidddleware::class, [SecondMidddleware::class, ['int' => 456, new \stdClass]]],
    'after'  => [FirstMidddleware::class, [SecondMidddleware::class, ['int' => 456, new \stdClass]]]
]);

/**
 * @Routing(url = '')
 * @Middleware(name = 'App\Middleware\FirstMidddleware')
 * @Middleware(name = 'App\Middleware\SecondMidddleware', params = {int : '456'})
 * @AfterMiddleware(name = 'App\Middleware\FirstMidddleware')
 * @AfterMiddleware(name = 'App\Middleware\SecondMidddleware', params = {int : '456'})
 */
public function read()

public function __invoke(array $params, array $middlewares)

public function __invoke(array $middlewares)

'before' => [FirstMidddleware::class, SecondMidddleware::class]],
'before' => [[FirstMidddleware::class], [SecondMidddleware::class]]

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

/**
 * @Routing(url = 'create/:id', method = 'POST')
 */
public function create($id)

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

/**
 * @Routing(url = 'update/:id', method = 'PUT')
 */
public function update($id)

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

/**
 * @Routing(url = 'update/:id', method = 'PATCH')
 */
public function update($id)

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

/**
 * @Routing(url = 'delete/:id', method = 'DELETE')
 */
public function delete($id)

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

/**
 * @Routing(url = 'any/:id', method = 'GET|POST|PUT|PATCH|DELETE')
 */
public function any($id)

$router->resource('api/:id', MainController::class);

$router->resource('api/:id', MainController::class, ['actionIndex', 'actionAdd', 'actionUpdate', 'actionDrop']);

$router->set(['/test/page', 'POST|PUT', function () {
            echo 'Hello world!';
        }
]);

$router->set(['/test/:id', 'DELETE|PUT', [MainController::class, 'actionIndex'], [
        'before' => [First::class, Second::class],
        'after'  => [[First::class], [Second::class]]
]]);



namespace App\Middleware;

use Rudra\Router\Router;
use Rudra\Router\MiddlewareInterface;

class FirstMiddleware extends Router implements MiddlewareInterface
{
    public function __invoke(array $middlewares)
    {
        $this->next($middlewares);
    }

    public function next(array $middlewares): void
    {
        $this->handleMiddleware($middlewares);
    }
}



namespace App\Middleware;

use Rudra\Router\MiddlewareInterface;
use Rudra\Router\RouterFacade as Router;

class SecondMiddleware implements MiddlewareInterface
{
    public function __invoke(array $middlewares, array $params)
    {
        var_dump($params);
        $this->next($middlewares);
    }
    
    public function next(array $middlewares): void
    {
        Router::handleMiddleware($middlewares);
    }
}