1. Go to this page and download the library: Download iassasin/easyroute 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/ */
iassasin / easyroute example snippets
use Iassasin\Easyroute\Router;
use Iassasin\Easyroute\Route;
$router = new Router();
$router->setControllersPath($_SERVER['DOCUMENT_ROOT'].'/controllers/');
$router->addRoutes([
new Route('/(:controller:(/:action:(/:arg)?)?)?', ['controller' => 'home', 'action' => 'index', 'arg' => null]),
]);
$router->processRoute();
class ControllerHome {
public function index($arg){
return '<html><body>Home page! '.($arg !== null ? 'Argument: '.$arg : 'Argument not set').'</body></html>';
// or use "return new Response('...')" which is more flexible
}
}
new Route('/:arg(\d+)?', // arg not => 'index', 'arg' => null], // default values
)
class ControllerHome {
public function index($arg, Request $request, DatabaseService $db){
// ...
}
}
use Iassasin\Easyroute\Router;
$router = new Router();
/** @var SimpleContainer $container */
$container = $router->getContainer(); // get default container
// Instantiate and add services by hand
$container->setService(Database::class, new Database('login', 'password'));
// Disable automatic instantiation for services
$container->setAutowireEnabled(false);
use Iassasin\Easyroute\Router;
use Iassasin\Easyroute\Http\Request;
$router = new Router();
$request = Request::createFromGlobals();
// some implementation of ContainerInterface
$container = new MyContainer();
// Router don't know how to register Request class in your container implementation
// You should to do it by self if you want to use Request class in controllers
// See next section for details of Request class
$container->register(Request::class, $request);
$router->setContainer($container);
use Iassasin\Easyroute\Http\Responses\Response404;
$router->setResponseHandler(Response404::class, function(Response404 $resp){
$resp->setContent('<html><body><h1>Custom 404 Not Found</h1> The requested url "<i>'.htmlspecialchars($resp->getUrl()).'</i>" not found!');
$resp->send();
return true; // do not call other handlers
});
use Iassasin\Easyroute\Http\Response;
$router->setStatusHandler(302, function(Response $resp){
$resp->setContent('You have redirected');
$resp->send();
return true; // do not call other handlers
});
(new Route('/admin/:controller/:action?', ['action' => 'index']))
->setControllersSubpath('zones/admin')
// '/admin/home/index' will match 'controllers/zones/admin/home.php'
// and class 'ControllerHome'
use Iassasin\Easyroute\RouteFilter;
class RouteFilterAdmin extends RouteFilter {
public function preRoute($path, $controller, $action, $args){
if (!isCurrentUserAdmin()){
(new Response('Access denied!', 403))->send();
return Router::COMPLETED; // Do not call controller's action
}
return Router::CONTINUE; // Call controller's action
}
}
//...
(new Route('/admin/:controller/:action?', ['action' => 'index']))
->setFilter(new RouteFilterAdmin())