1. Go to this page and download the library: Download monomelodies/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/ */
use Psr\Http\Message\RequestInterface;
$router->when('/some/url/')->then(function (RequestInterface $request) {
switch ($request->getMethod()) {
case 'POST':
// Perform some action
case 'GET':
return 'ok';
default:
return $request->getMethod()." method not allowed.";
}
});
use Zend\Diactoros\Response\EmptyResponse;
$router->when('/some/url/')->then('my-awesome-state', function () {
// Get not allowed!
return new EmptyResponse(403);
})->post(function () {
// ...do something, POST is allowed...
// Since we disabled get, this should redirect somewhere valid afterwards.
});
$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/', function ($router) {
$router->then('I match /foo/!');
$router->when('/bar/')->then('I match /foo/bar/!');
});
$router->when('/foo/')->when('/bar/')->then('I match /foo/bar/!');
// ...or...
$foo = $router->when('/foo/');
$foo->when('/bar/')->then('I match /foo/bar/!');
$router->when('/foo/')
->then('I match /foo/!')
->when('/bar/')
->then('But I match /foo/bar/!');
$router->when('/restricted/')
->pipe(function ($payload) {
if (!user_is_authenticated()) {
// In the real world, probably raise an exception you can
// catch elsewhere and show a login page or something...
return null;
}
return $payload;
})
->when('/super-secret-page/')
->then('For authenticated eyes only!');
$router->when("/(?'foo':\d+)/")
->pipe(function ($payload, $foo) {
if ($foo != 42) {
// return error response or something...
}
return $payload;
});