<?php // example.com/web/front.php require_once __DIR__.'/../src/autoload.php'; use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Response; $request = Request::createFromGlobals(); $map = array( '/hello' => 'hello', '/bye' => 'bye', ); $path = $request->getPathInfo(); if (isset($map[$path])) { ob_start(); extract($request->query->all(), EXTR_SKIP); include sprintf(__DIR__.'/../src/pages/%s.php', $map[$path]); $response = new Response(ob_get_clean()); } else { $response = new Response('Not Found', 404); } $response->send(); <!-- example.com/src/pages/hello.php --> Hello <?php echo htmlspecialchars($name, ENT_QUOTES, 'UTF-8') ?> # Before /hello?name=Fabien # After /hello/Fabien php composer.phar update command to install it: { "require": { "symfony/class-loader": "2.1.*", "symfony/http-foundation": "2.1.*", "symfony/routing": "2.1.*" } } <?php // example.com/web/front.php require_once __DIR__.'/../vendor/.composer/autoload.php'; // ... use Symfony\Component\Routing\RouteCollection; $routes = new RouteCollection(); use Symfony\Component\Routing\Route; $routes->add('hello', new Route('/hello/{name}', array('name' => 'World'))); $routes->add('bye', new Route('/bye')); (/hello/{name}) and the array of default attributes (array('name' => 'World')) . use Symfony\Component\Routing\RequestContext; use Symfony\Component\Routing\Matcher\UrlMatcher; $context = new RequestContext(); $context->fromRequest($request); $matcher = new UrlMatcher($routes, $context); $attributes = $matcher->match($request->getPathInfo()); print_r($matcher->match('/bye')); array ( '_route' => 'bye', ); print_r($matcher->match('/hello/Fabien')); array ( 'name' => 'Fabien', '_route' => 'hello', ); print_r($matcher->match('/hello')); array ( 'name' => 'World', '_route' => 'hello', ); $matcher->match('/not-found'); // throws a Symfony\Component\Routing\Exception\ResourceNotFoundException <?php // example.com/web/front.php require_once __DIR__.'/../vendor/.composer/autoload.php'; use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Response; use Symfony\Component\Routing; $request = Request::createFromGlobals(); $routes = include __DIR__.'/../src/app.php'; $context = new Routing\RequestContext(); $context->fromRequest($request); $matcher = new Routing\Matcher\UrlMatcher($routes, $context); try { extract($matcher->match($request->getPathInfo()), EXTR_SKIP); ob_start(); include sprintf(__DIR__.'/../src/pages/%s.php', $_route); $response = new Response(ob_get_clean()); } catch (Routing\Exception\ResourceNotFoundException $e) { $response = new Response('Not Found', 404); } catch (Exception $e) { $response = new Response('An error occurred', 500); } $response->send(); <!-- example.com/src/pages/hello.php --> Hello <?php echo htmlspecialchars($name, ENT_QUOTES, 'UTF-8') ?> <?php // example.com/src/app.php use Symfony\Component\Routing; $routes = new Routing\RouteCollection(); $routes->add('hello', new Routing\Route('/hello/{name}', array('name' => 'World'))); $routes->add('bye', new Routing\Route('/bye')); return $routes; use Symfony\Component\Routing; $generator = new Routing\Generator\UrlGenerator($routes, $context); echo $generator->generate('hello', array('name' => 'Fabien')); // outputs /hello/Fabien echo $generator->generate('hello', array('name' => 'Fabien'), true); // outputs something like http://example.com/somewhere/hello/Fabien $dumper = new Routing\Matcher\Dumper\PhpMatcherDumper($routes); echo $dumper->dump(); $dumper = new Routing\Matcher\Dumper\ApacheMatcherDumper($routes); echo $dumper->dump(); Source: https://habr.com/ru/post/136656/
All Articles