1. Go to this page and download the library: Download crysalead/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/ */
crysalead / router example snippets
use Lead\Router\Router;
$router = new Router();
$router->basePath('/my/sub/dir');
$router->basePath(); // Gets/sets the router base path
$router->group(); // To create some scoped routes
$router->bind(); // To create a route
$router->route(); // To route a request
$router->link(); // To generate a route's link
$router->apply(); // To add a global middleware
$router->middleware(); // The router's middleware generator
$router->strategy(); // Gets/sets a routing strategy
use Lead\Router\Router;
$router = new Router();
$router->bind($pattern, $handler); // route matching any request method
$router->bind($pattern, $options, $handler); // alternative syntax with some options.
$router->bind($pattern, ['methods' => 'GET'], $handler); // route matching on only GET requests
$router->bind($pattern, ['methods' => ['POST', 'PUT']], $handler); // route matching on POST and PUT requests
// Alternative syntax
$router->get($pattern, $handler); // route matching only get requests
$router->post($pattern, $handler); // route matching only post requests
$router->delete($pattern, $handler); // route matching only delete requests
$route->method; // The method contraint
$route->params; // The matched params
$route->persist; // The persisted params
$route->namespace; // The namespace
$route->name; // The route's name
$route->request; // The routed request
$route->response; // The response (same as 2nd argument, can be `null`)
$route->dispatched; // To store the dispated instance if applicable.
$route->host(); // The route's host instance
$route->pattern(); // The pattern
$route->regex(); // The regex
$route->variables(); // The variables
$route->token(); // The route's pattern token structure
$route->scope(); // The route's scope
$route->error(); // The route's error number
$route->message(); // The route's error message
$route->link(); // The route's link
$route->apply(); // To add a new middleware
$route->middleware(); // The route's middleware generator
$route->handler(); // The route's handler
$route->dispatch(); // To dispatch the route (i.e execute the route's handler)
use Lead\Router\Router;
$router = new Router();
// Bind to all methods
$router->bind('foo/bar', function() {
return "Hello World!";
});
// Bind to POST and PUT at dev.example.com only
$router->bind('foo/bar/edit', ['methods' => ['POST',' PUT'], 'host' => 'dev.example.com'], function() {
return "Hello World!!";
});
// The Router class makes no assumption of the ingoing request, so you have to pass
// uri, methods, host, and protocol into `->route()` or use a PSR-7 Compatible Request.
// Do not rely on $_SERVER, you must check or sanitize it!
$route = $router->route(
$_SERVER['REQUEST_URI'], // foo/bar
$_SERVER['REQUEST_METHOD'], // get, post, put...etc
$_SERVER['HTTP_HOST'], // www.example.com
$_SERVER['SERVER_PROTOCOL'] // http or https
);
echo $route->dispatch(); // Can throw an exception if the route is not valid.
use Lead\Router\Router;
use Lead\Net\Http\Cgi\Request;
use Lead\Net\Http\Response;
$request = Request::ingoing();
$response = new Response();
$router = new Router();
$router->bind('foo/bar', function($route, $response) {
$response->body("Hello World!");
return $response;
});
$route = $router->route($request);
echo $route->dispatch($response); // Can throw an exception if the route is not valid.
use Lead\Router\RouterException;
use Lead\Router\Router;
use Lead\Net\Http\Cgi\Request;
use Lead\Net\Http\Response;
$request = Request::ingoing();
$response = new Response();
$router = new Router();
$router->bind('foo/bar', function($route, $response) {
$response->body("Hello World!");
return $response;
});
$route = $router->route($request);
try {
echo $route->dispatch($response);
} catch (RouterException $e) {
http_response_code($e->getCode());
// Or you can use Whoops or whatever to render something
}
use Lead\Router\Router;
use My\Custom\Namespace\ResourceStrategy;
Router::strategy('resource', new ResourceStrategy());
$router = new Router();
$router->resource('Home', ['namespace' => 'App\Resource']);
// Now all the following URL can be routed
$router->route('home');
$router->route('home/123');
$router->route('home/add');
$router->route('home', 'POST');
$router->route('home/123/edit');
$router->route('home/123', 'PATCH');
$router->route('home/123', 'DELETE');