<?php // example.com/web/front.php // ... try { $request->attributes->add($matcher->match($request->getPathInfo())); $response = call_user_func('render_template', $request); } catch (Routing\Exception\ResourceNotFoundException $e) { $response = new Response('Not Found', 404); } catch (Exception $e) { $response = new Response('An error occurred', 500); }
function render_template($request) { extract($request->attributes->all(), EXTR_SKIP); ob_start(); include sprintf(__DIR__.'/../src/pages/%s.php', $_route); return new Response(ob_get_clean()); }
$routes->add('hello', new Routing\Route('/hello/{name}', array( 'name' => 'World', '_controller' => 'render_template', ))); try { $request->attributes->add($matcher->match($request->getPathInfo())); $response = call_user_func($request->attributes->get('_controller'), $request); } catch (Routing\Exception\ResourceNotFoundException $e) { $response = new Response('Not Found', 404); } catch (Exception $e) { $response = new Response('An error occurred', 500); }
$routes->add('hello', new Routing\Route('/hello/{name}', array( 'name' => 'World', '_controller' => function ($request) { return render_template($request); } )));
$routes->add('hello', new Routing\Route('/hello/{name}', array( 'name' => 'World', '_controller' => function ($request) { // $foo will be available in the template $request->attributes->set('foo', 'bar'); $response = render_template($request); // change some header $response->headers->set('Content-Type', 'text/plain'); return $response; } )));
<?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; function render_template($request) { extract($request->attributes->all(), EXTR_SKIP); ob_start(); include sprintf(__DIR__.'/../src/pages/%s.php', $_route); return new Response(ob_get_clean()); } $request = Request::createFromGlobals(); $routes = include __DIR__.'/../src/app.php'; $context = new Routing\RequestContext(); $context->fromRequest($request); $matcher = new Routing\Matcher\UrlMatcher($routes, $context); try { $request->attributes->add($matcher->match($request->getPathInfo())); $response = call_user_func($request->attributes->get('_controller'), $request); } catch (Routing\Exception\ResourceNotFoundException $e) { $response = new Response('Not Found', 404); } catch (Exception $e) { $response = new Response('An error occurred', 500); } $response->send();
<?php // example.com/src/app.php use Symfony\Component\Routing; use Symfony\Component\HttpFoundation\Response; function is_leap_year($year = null) { if (null === $year) { $year = date('Y'); } return 0 == $year % 400 || (0 == $year % 4 && 0 != $year % 100); } $routes = new Routing\RouteCollection(); $routes->add('leap_year', new Routing\Route('/is_leap_year/{year}', array( 'year' => null, '_controller' => function ($request) { if (is_leap_year($request->attributes->get('year'))) { return new Response('Yep, this is a leap year!'); } return new Response('Nope, this is not a leap year.'); } ))); return $routes;
Source: https://habr.com/ru/post/138010/
All Articles