1. Go to this page and download the library: Download monolyth/reroute 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/ */
monolyth / reroute example snippets
use Monolyth\Reroute\Router;
$router = new Router('http://example.com');
$state = $router->when('/some/url/', 'some-state');
use Zend\Diactoros\Response\HtmlResponse;
$router->when('/some/url/')->get(function () {
return new HtmlsReponse('Hello world!');
});
$router->when('/some/url/')->get(new HtmlResponse('Hello world!'));
class Foo
{
public static function getInstance()
{
return new Foo;
}
public function __invoke()
{
return new HtmlResponse('Hello world!');
}
}
$router->when('/some/url/')->get(new Foo);
$router->when('/some/url/')->get(Foo::class);
$router->when('/some/url/')->get(['Foo', 'getInstance']);
use Zend\Diactoros\ServerRequestFactory;
use Zend\Diactoros\Response\SapiEmitter;
if ($response = $router(ServerRequestFactory::fromGlobals())) {
$emitted = new SapiEmitter;
$emitter->emit($response);
} else {
// 404!
}
use Psr\Http\Message\RequestInterface;
use Zend\Diactoros\Response\HtmlResponse;
$router->when('/some/url/')->any(function (RequestInterface $request) {
switch ($request->getMethod()) {
case 'POST':
// Perform some action
case 'GET':
return new HtmlResponse('ok');
default:
return new HtmlResponse($request->getMethod()." method not allowed.", 405);
}
});
$router->when('/some/url/')->then('my-state', function() {
return 'This is a normal page';
})->post(function (callable $GET) {
// Perform some action...
return $GET;
});
$router->when('/foo/', null, function ($router) {
$router->when('/bar/')->get('I match /foo/bar/!');
})->get('I match /foo/!);
$router->when('/', 'home', function ($router) {
// Looooong list of subroutes under /...
})->pipe($somePipe)->get('Home!');
$router->when('/', null, function ($router) {
$router->when('/', 'home')->get('Home!');
// Looooong list of subroutes under /...
})->pipe($somePipe);
$router->when('/restricted/')
->pipe(function ($payload) {
if (!user_is_authenticated()) {
return new RedirectResponse('/login/');
}
return $payload;
})
->get(new Zend\Diactoros\Response\HtmlResponse('For authenticated eyes only!'));
$router->when("/(?'foo':\d+)/")
->pipe(function ($payload, int $foo) {
if ($foo != 42) {
// return error response or something...
}
return $payload;
});
use Zend\Diactoros\Response\RedirectResponse;
$router->when("/(?'language'[a-z]{2})/", null, function ($router) {
$router->when('/', 'home')->get(function () { /* some page */ });
$router->when('/home/')->get(function () use ($router) {
return new RedirectResponse($router->generate('home'));
});
});
// Now, assuming we navigate to `/en/home/` we get redirected to `/en/`!
$router->when("/user/(?'id'\d+/)?")->then(function ($id = null) {
if (!isset($id)) {
$id = $GLOBALS['user']->id;
}
// ...return profile for $id...
return "<h1>User profile for $id</h1>";
});