1. Go to this page and download the library: Download idealogica/route-one 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/ */
idealogica / route-one example snippets
// general dispatcher
$dispatcher = DispatcherFactory::createDefault()->createDispatcher();
// page layout middleware
$dispatcher->addMiddleware(
function (ServerRequestInterface $request, DelegateInterface $next) {
$response = $next->process($request);
$content = $response->getBody()->getContents();
return $response
->withBody($this->streamFor('<html><body>' . $content . '</body></html>'))
->withHeader('content-type', 'text/html; charset=utf-8');
}
);
// blog middleware
$dispatcher->addMiddleware(
function (ServerRequestInterface $request, DelegateInterface $next) {
// blog middleware dispatcher
$blogDispatcher = DispatcherFactory::createDefault()->createDispatcher();
$blogDispatcher->getDefaultRoute()->setHost('www.test.com')->setSecure(false);
// blog posts list middleware (path based routing)
$blogDispatcher->addGetRoute('/blog/posts',
function (ServerRequestInterface $request, DelegateInterface $next) {
// stop middleware chain execution
return new Response($this->streamFor('<h1>Posts list</h1><p>Post1</p><p>Post2</p>'));
}
)->setName('blog.list');
// blog single post middleware (path based routing)
$blogDispatcher->addGetRoute('/blog/posts/{id}',
function (ServerRequestInterface $request, DelegateInterface $next) {
$id = (int)$request->getAttribute('1.id'); // prefix for route-one attributes
// post id is valid
if ($id === 1) {
// stop middleware chain execution
return new Response($this->streamFor(sprintf('<h1>Post #%s</h1><p>Example post</p>', $id)));
}
// post not found, continue to the next middleware
return $next->process($request);
}
)->setName('blog.post');
// blog page not found middleware (no routing, executes for each request)
$blogDispatcher->addMiddleware(
function (ServerRequestInterface $request, DelegateInterface $next)
{
// 404 response
return new Response($this->streamFor('<h1>Page not found</h1>'), 404);
}
);
return $blogDispatcher->dispatch($request);
}
);
// dispatching
$response = $dispatcher->dispatch(
new ServerRequest(
[],
[],
'http://www.test.com/blog/posts/1',
'GET'
)
);
(new SapiEmitter())->emit($response);
$routeFactory = new RouteFactory(
new AuraRouteMiddleware($basePath),
new AuraUriGenerator($basePath)
);
$routeMiddleware = $routeFactory->createGetRoute('/blog/posts', $middlewareToRoute);
// route path is '/blog/posts/{id}' for 'blog.post' route
$blogPostUrl = $blogDispatcher->getUriGenerator()->generate('blog.post', ['id' => 100]);
echo($blogPostUrl); // outputs "http://www.test.com/blog/posts/100"
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.