Symfony Con, Paris,
December 3rd, 2015
var
`-- www
`-- htdocs
|-- index.htm
|-- about.htm
`-- news.htm
RewriteRule ^(.*)$ app.php [QSA,L]
switch($path) {
case '/':
return $controller->renderHomepage();
case '/about':
return $controller->renderAbout();
default:
return $controller->render404();
}
Request object to array with routing information
public interface RequestMatcherInterface
{
/**
* @param Request $request request to match
*
* @return array An array of parameters
*
* @throws ResourceNotFoundException
* @throws MethodNotAllowedException
*/
public function matchRequest(Request $request);
}
/foo/x/{id}
/foo/y/{id}
/bar/baz
/foo
+---/x/
| `--{id}
`---/y/
`--{id}
/bar/baz
$context = $this->router->getContext();
$context->setHost('example.com');
$context->setScheme('https');
$context->setBaseUrl('my/path');
$this->router->generate(...);
Symfony documentation
# app/config/routing.yml
admin_routes:
type: service
resource: "admin_route_loader:loadRoutes"
# app/config/routing.yml
product:
type: rest
resource: AppBundle\Controller\ProductController
# routing.yml
homepage:
path: /welcome
defaults: { _controller: MyWebsiteBundle:Frontend:index }
# translations/routes.de.yml
homepage: /willkommen
type: be_simple_i18n
...
homepage:
locales:
en: /welcome
de: /willkommen
defaults: { _controller: MyWebsiteBundle:Frontend:index }
kernel.request at priority 32
ParamConverter
http://symfony.com/doc/current/components/http_kernel/introduction.html
kernel.controller event
function onKernelRequest(GetResponseEvent $event) {
$accept = $event->getRequest()
->headers->get('accept');
if ('user-context-hash' !== $accept) return;
$hash = $this->hashGenerator->generateHash();
$response = new Response('', 200, array(
$this->hashHeader => $hash,
'Content-Type' => 'user-context-hash',
));
$event->setResponse($response);
}
my_route:
path: /welcome
defaults:
_controller: my_service:fooAction
theme_controllers:
a: my_other_service:fooAction
b: App:Other:foo
function onKernelRequest(GetResponseEvent $event) {
$request = $event->getRequest();
$theme = $this->activeTheme->getName();
$controllers = $request->attributes
->get('theme_controllers');
if (isset($controllers[$theme])) {
$request->attributes->set(
'_controller',
$controllers[$theme]
);
}
}
acme_demo_home:
path: /
options:
breadcrumb:
label: Home
acme_demo_contact:
path: /contact
options:
breadcrumb:
label: Contact
parent_route: acme_demo_home