The HttpKernel Component: the Controller Resolver

You might think that our framework is already pretty solid and you are
probably right. But let's see how we can improve it nonetheless.

Right now, all our examples use procedural code, but remember that controllers
can be any valid PHP callbacks. Let's convert our controller to a proper
class:

1
2
3
4
5
6
7
8
9
10
11

classLeapYearController{publicfunctionindex($request){if(is_leap_year($request->attributes->get('year'))){returnnewResponse('Yep, this is a leap year!');}returnnewResponse('Nope, this is not a leap year.');}}

The move is pretty straightforward and makes a lot of sense as soon as you
create more pages but you might have noticed a non-desirable side-effect...
The LeapYearController class is always instantiated, even if the
requested URL does not match the leap_year route. This is bad for one main
reason: performance wise, all controllers for all routes must now be
instantiated for every request. It would be better if controllers were
lazy-loaded so that only the controller associated with the matched route is
instantiated.

To solve this issue, and a bunch more, let's install and use the HttpKernel
component:

1

$ composer require symfony/http-kernel

The HttpKernel component has many interesting features, but the ones we need
right now are the controller resolver and argument resolver. A controller resolver knows how to
determine the controller to execute and the argument resolver determines the arguments to pass to it,
based on a Request object. All controller resolvers implement the following interface:

The getController() method relies on the same convention as the one we
have defined earlier: the _controller request attribute must contain the
controller associated with the Request. Besides the built-in PHP callbacks,
getController() also supports strings composed of a class name followed by
two colons and a method name as a valid callback, like 'class::method':

As an added bonus, the controller resolver properly handles the error
management for you: when you forget to define a _controller attribute
for a Route for instance.

Now, let's see how the controller arguments are guessed. getArguments()
introspects the controller signature to determine which arguments to pass to
it by using the native PHP reflection. This method is defined in the
following interface:

Finally, you can also define default values for any argument that matches an
optional attribute of the Request:

publicfunctionindex($year=2012)

Let's inject the $year request attribute for our controller:

1
2
3
4
5
6
7
8
9
10
11

classLeapYearController{publicfunctionindex($year){if(is_leap_year($year)){returnnewResponse('Yep, this is a leap year!');}returnnewResponse('Nope, this is not a leap year.');}}

The resolvers also take care of validating the controller callable and its
arguments. In case of a problem, it throws an exception with a nice message
explaining the problem (the controller class does not exist, the method is not
defined, an argument has no matching attribute, ...).

Note

With the great flexibility of the default controller resolver and argument
resolver, you might wonder why someone would want to create another one
(why would there be an interface if not?). Two examples: in Symfony,
getController() is enhanced to support controllers as services;
and getArguments() provides an extension point to alter or enhance
the resolving of arguments.