1. Go to this page and download the library: Download delolmo/symfony-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/ */
delolmo / symfony-router example snippets
use Laminas\Diactoros\Response\HtmlResponse;
use Laminas\Diactoros\ServerRequest;
use Middlewares\Utils\Dispatcher;
use Middlewares\Utils\Factory;
use Symfony\Component\Config\FileLocator;
use Symfony\Component\Routing\Loader\PhpFileLoader;
use Symfony\Component\Routing\RequestContext;
use Symfony\Component\Routing\Router;
$fileLocator = new FileLocator(array(__DIR__));
$router = new Router(
new PhpFileLoader($fileLocator),
'routes.php',
array('cache_dir' => __DIR__ . '/cache'),
new RequestContext('/')
);
$factory = Factory::getRequestFactory();
$dispatcher = new Dispatcher([
new DelOlmo\Middleware\SymfonyRouterMiddleware($router, $factory),
function($request, $next) {
return new HtmlResponse(json_encode($request->getAttributes()));
}
]);
// Try matching a /blog request
$response = $dispatcher->dispatch(new ServerRequest([], [], '/blog'));
// Will return {"_route": "blog_list", "request-handler" => ["App\Controller\BlogController", "list"]}
$c->get('emitter')->emit($response);
// Try matching a /blog/hello-world request
$response = $dispatcher->dispatch(new ServerRequest([], [], '/blog/hello-world'));
// Will return {"_route": "blog_show", "request-handler" => ["App\Controller\BlogController", "show"], "slug" => "hello-world"}
$c->get('emitter')->emit($response);