Confoo Montreal,
February 24th, 2022
var
`-- www
`-- htdocs
|-- index.htm
|-- about.htm
`-- news.htm
RewriteRule ^(.*)$ app.php [QSA,L]
What PHP code to call for an URL?
switch($path) {
case '/':
return $controller->renderHomepage();
case '/about':
return $controller->renderAbout();
default:
return $controller->render404();
}
Request object to hashmap with routing information
public interface RequestMatcherInterface
{
/**
* @return array<string, string>
*/
public function matchRequest(
Request $request
): array;
}
/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.yaml
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
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]
);
}
}
ParamConverterhttps://symfony.com/doc/current/components/http_kernel.html
kernel.controller event