PHP code example of eserozvataf / scabbia2-router

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

    

eserozvataf / scabbia2-router example snippets


use Scabbia\Router\RouteCollection;

$routes = new RouteCollection();

// adding a static route
$routes->addRoute('GET', '/about', 'AboutController::IndexAction');

// adding a static route with multiple http methods
$routes->addRoute(['GET', 'POST'], '/about', 'AboutController::IndexAction');

// adding a dynamic route
$routes->addRoute('GET', '/users/profile/{id:[a-z]+}', 'UsersController::ProfileAction');

// adding a dynamic route with a routing name
$routes->addRoute('GET', '/users/posts/{id:[a-z]+}', 'UsersController::PostsAction', 'user/posts');

file_put_contents('routes.json', json_encode($routes->save()));

$routes = json_decode(file_get_contents('routes.json'));

use Scabbia\Router\Router;

$router = new Router($routes); // initialize a new router with route definitions
$route = $router->dispatch('GET', '/about');

if ($route['status'] === Router::FOUND) {
  call_user_func($route['callback'], ...$route['parameters']);
}

use Scabbia\Router\Router;

$router = new Router($routes); // initialize a new router with route definitions

echo $router->path('users/posts', [ 'id' => 'eser' ]);