1. Go to this page and download the library: Download raylin666/routing 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/ */
raylin666 / routing example snippets
use Raylin666\Http\Request;
use Raylin666\Router\RouteCollection;
$collection = new RouteCollection();
$collection->addRoute('GET', '/', function () {
return 'hello world';
});
$route = $collection->match(new Request('GET', '/')); // \Raylin666\Router\Route
echo call_user_func_array($route->getCallback(), []);
use Raylin666\Http\Request;
use Raylin666\Router\RouteCollection;
$collection = new RouteCollection();
$collection->addRoute('GET', '/{name}', function ($name) {
return 'hello ' . $name;
});
$route = $collection->match(new Request('GET', '/foo')); // \Raylin666\Router\Route
echo call_user_func_array($route->getCallback(), $route->getParameters());
use Raylin666\Router\RouteCollection;
use Raylin666\Router\RouteDispatcher;
use Raylin666\Http\Request;
$collection = new RouteCollection();
$collection->get('/', function () {
return 'hello GET';
});
$collection->post('/', function () {
return 'hello POST';
});
$dispatcher = new RouteDispatcher($collection);
$response = $dispatcher->dispatch(new Request('GET', '/')); // hello GET
dump($response);
$response = $dispatcher->dispatch(new Request('POST', '/')); // hello POST
dump($response);
use Raylin666\Http\Request;
use Raylin666\Router\RouteCollection;
$collection = new RouteCollection();
$collection->addRoute('GET', '/{name}', function () {
return 'get1';
});
$collection->addRoute('GET', '/', function () {
return 'get2';
});
$route = $collection->match(new Request('GET', '/abc')); // \Raylin666\Router\Route
$route2 = $collection->match(new Request('GET', '/')); // \Raylin666\Router\Route
echo call_user_func_array($route->getCallback(), $route->getParameters()); // get1
echo call_user_func_array($route2->getCallback(), $route2->getParameters()); // get2
use Raylin666\Http\Request;
use Raylin666\Router\RouteCollection;
$collection = new RouteCollection();
$collection->group('/v1', function (RouteCollection $collection) {
$collection->addRoute('GET', '/{name}', function () {
return 'get1';
});
});
$route = $collection->match(new Request('GET', '/v1/abc'));
echo call_user_func_array($route->getCallback(), $route->getParameters()); // get1
use Raylin666\Http\Request;
use Raylin666\Router\RouteCollection;
$collection = new RouteCollection();
$collection->addRoute('GET', '/api/*', function ($path) {
return $path;
});
$route = $collection->match(new Request('GET', '/api/abc'));
echo call_user_func_array($route->getCallback(), $route->getParameters()); // /abc
$route = $collection->match(new Request('GET', '/api/cba'));
echo call_user_func_array($route->getCallback(), $route->getParameters()); // /cba
use Raylin666\Http\Request;
use Raylin666\Router\RouteCollection;
use Raylin666\Router\RouteDispatcher;
use Raylin666\Middleware\Middleware;
use Psr\Http\Message\ServerRequestInterface;
use Raylin666\Middleware\DelegateInterface;
class HttpMiddleware extends Middleware
{
public function handler(ServerRequestInterface $request, DelegateInterface $next)
{
// TODO: Implement handle() method.
return $next->process($request);
}
}
$router = new RouteCollection('\\App\\Http\\Controllers\\');
$dispatcher = new RouteDispatcher($router, [
'httpMiddleware' => new HttpMiddleware()
]);
$router->group(['prefix' => '', 'middleware' => 'httpMiddleware'], function () use ($router) {
$router->group(['prefix' => 'v1'], function() use ($router) {
$router->get('/hello', function () {
return 'hello world';
} /*'v1\CommonController@Hello'*/);
});
});
$response = $dispatcher->dispatch(new Request('GET', '/v1/hello'));
dump($response); // hello world
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.