1. Go to this page and download the library: Download stratify/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/ */
stratify / router example snippets
use Stratify\Http\Middleware\MiddlewarePipe;
use Stratify\Router\PrefixRouter;
use Stratify\Router\Router;
$app = new MiddlewarePipe([
new ErrorHandlerMiddleware(), // a middleware that applies to the whole stack
new PrefixRouter([
// The blog has its own router
'/blog/' => new Router([
'/blog/' => new HomeController(),
'/blog/{article}' => new ArticleController(),
]),
// The API has its own router with its own middlewares (authentication for example)
'/api/' => new MiddlewarePipe([
new HttpBasicAuthMiddleware(), // authentication only for the API
new Router([
'/api/articles' => new ArticleController(),
'/api/users' => new UserController(),
]),
]),
]),
]);
$router = new Router([
'/' => function (...) {
...
},
]);
'/article/{id}' => function ($id) {
...
},
'/category/{id}' => function (ServerRequestInterface $request) {
$id = $request->getAttribute('id');
...
},
use function \Stratify\Router\route;
$router = new Router([
'/{id}' => route(function () { … })
->pattern('id', '\d+') // the placeholder must be a number
]);
class ArticleController
{
public function index()
{
return new HtmlResponse('Index');
}
public function post()
{
return new HtmlResponse('POST');
}
public function get(ServerRequestInterface $request)
{
return new HtmlResponse('GET '.$request->getAttribute('id'));
}
public function put(ServerRequestInterface $request)
{
return new HtmlResponse('PUT '.$request->getAttribute('id'));
}
public function delete(ServerRequestInterface $request)
{
return new HtmlResponse('DELETE '.$request->getAttribute('id'));
}
}
$router = new PrefixRouter([
'/api/' => /* API middleware stack */,
'/admin/' => /* Admin middleware stack */,
'/' => /* Public website middleware stack */,
]);
function (ServerRequestInterface $request, callable $next) : ResponseInterface {
}
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.