8000 Documented the ArgumentResolver along the ControllerResolver · symfony/symfony-docs@00e0e7b · GitHub
[go: up one dir, main page]

Skip to content

Commit 00e0e7b

Browse files
author
Iltar van der Berg
committed
Documented the ArgumentResolver along the ControllerResolver
1 parent d7724dd commit 00e0e7b

File tree

1 file changed

+50
-19
lines changed

1 file changed

+50
-19
lines changed

create_framework/http_kernel_controller_resolver.rst

Lines changed: 50 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -43,10 +43,18 @@ component:
4343
4444
$ composer require symfony/http-kernel
4545
46-
The HttpKernel component has many interesting features, but the one we need
47-
right now is the *controller resolver*. A controller resolver knows how to
48-
determine the controller to execute and the arguments to pass to it, based on
49-
a Request object. All controller resolvers implement the following interface::
46+
The HttpKernel component has many interesting features, but the ones we need
47+
right now are the *controller resolver* and *argument resolver*. A controller resolver knows how to
48+
determine the controller to execute and the argument resolver determines the arguments to pass to it,
49+
based on a Request object. All controller resolvers implement the following interface
50+
51+
.. caution::
52+
53+
The `getArguments()` method in the :class:`Symfony\\Component\\Httpkernel\\Controller\\ControllerResolver`
54+
and respective interface :class:`Symfony\\Component\\Httpkernel\\Controller\\ControllerResolverInterface`
55+
are deprecated as of 3.1 and will be removed in 4.0. You can use the
56+
:class:`Symfony\\Component\\Httpkernel\\Controller\\ArgumentResolver` which uses the
57+
:class:`Symfony\\Component\\Httpkernel\\Controller\\ArgumentResolverInterface` instead.::
5058

5159
namespace Symfony\Component\HttpKernel\Controller;
5260

@@ -58,6 +66,12 @@ a Request object. All controller resolvers implement the following interface::
5866
function getArguments(Request $request, $controller);
5967
}
6068

69+
// ...
70+
interface ArgumentResolverInterface
71+
{
72+
function getArguments(Request $request, $controller);
73+
}
74+
6175
The ``getController()`` method relies on the same convention as the one we
6276
have defined earlier: the ``_controller`` request attribute must contain the
6377
controller associated with the Request. Besides the built-in PHP callbacks,
@@ -74,10 +88,14 @@ resolver from HttpKernel::
7488

7589
use Symfony\Component\HttpKernel;
7690

77-
$resolver = new HttpKernel\Controller\ControllerResolver();
91+
$valueResolvers = [/* array of ArgumentValueResolverInterface implemetations */];
92+
$argumentMetadataFactory = new Symfony\Component\HttpKernel\ControllerMetadata\ArgumentMetadataFactory();
93+
94+
$controllerResolver = new HttpKernel\Controller\ControllerResolver();
95+
$argumentResolver = new HttpKernel\Controller\ArgumentResolver($argumentMetadataFactory, $valueResolvers);
7896

79-
$controller = $resolver->getController($request);
80-
$arguments = $resolver->getArguments($request, $controller);
97+
$controller = $controllerResolver->getController($request);
98+
$arguments = $argumentResolver->getArguments($request, $controller);
8199

82100
$response = call_user_func_array($controller, $arguments);
83101

@@ -140,20 +158,19 @@ method is not defined, an argument has no matching attribute, ...).
140158

141159
.. note::
142160

143-
With the great flexibility of the default controller resolver, you might
144-
wonder why someone would want to create another one (why would there be an
145-
interface if not?). Two examples: in Symfony, ``getController()`` is
146-
enhanced to support
147-
:doc:`controllers as services </cookbook/controller/service>`; and in
148-
`FrameworkExtraBundle`_, ``getArguments()`` is enhanced to support
149-
parameter converters, where request attributes are converted to objects
150-
automatically.
161+
With the great flexibility of the default controller resolver and argument
162+
resolver, you might wonder why someone would want to create another one
163+
(why would there be an interface if not?). Two examples: in Symfony,
164+
``getController()`` is enhanced to support :doc:`controllers as services </cookbook/controller/service>`;
165+
and ``getArguments()`` provides an extension point to alter or enhance
166+
the resolving of arguments.
151167

152168
Let's conclude with the new version of our framework::
153169

154170
// example.com/web/front.php
155171
require_once __DIR__.'/../vendor/autoload.php';
156172

173+
use Symfony\Component\HttpKernel\Controller\ArgumentValueResolver as ArgumentValueResolver;
157174
use Symfony\Component\HttpFoundation\Request;
158175
use Symfony\Component\HttpFoundation\Response;
159176
use Symfony\Component\Routing;
@@ -174,13 +191,27 @@ Let's conclude with the new version of our framework::
174191
$context = new Routing\RequestContext();
175192
$context->fromRequest($request);
176193
$matcher = new Routing\Matcher\UrlMatcher($routes, $context);
177-
$resolver = new HttpKernel\Controller\ControllerResolver();
194+
195+
$valueResolvers = [
196+
new ArgumentValueResolver\ArgumentFromAttributeResolver(),
197+
new ArgumentValueResolver\VariadicArgumentValueResolver(),
198+
new ArgumentValueResolver\RequestResolver(),
199+
new ArgumentValueResolver\DefaultArgumentValueResolver(),
200+
];
201+
202+
$argumentMetadataFactory = new Symfony\Component\HttpKernel\ControllerMetadata\ArgumentMetadataFactory();
203+
204+
$controllerResolver = new HttpKernel\Controller\ControllerResolver();
205+
$argumentResolver = new HttpKernel\Controller\ArgumentResolver($argumentMetadataFactory, $valueResolvers);
206+
207+
$controller = $controllerResolver->getController($request);
208+
$arguments = $argumentResolver->getArguments($request, $controller);
178209

179210
try {
180211
$request->attributes->add($matcher->match($request->getPathInfo()));
181212

182-
$controller = $resolver->getController($request);
183-
$arguments = $resolver->getArguments($request, $controller);
213+
$controller = $controllerResolver->getController($request);
214+
$arguments = $argumentResolver->getArguments($request, $controller);
184215

185216
$response = call_user_func_array($controller, $arguments);
186217
} catch (Routing\Exception\ResourceNotFoundException $e) {
@@ -192,7 +223,7 @@ Let's conclude with the new version of our framework::
192223
$response->send();
193224

194225
Think about it once more: our framework is more robust and more flexible than
195-
ever and it still has less than 40 lines of code.
226+
ever and it still has less than 60 lines of code.
196227

197228
.. _`reflection`: http://php.net/reflection
198229
.. _`FrameworkExtraBundle`: http://symfony.com/doc/current/bundles/SensioFrameworkExtraBundle/annotations/converters.html

0 commit comments

Comments
 (0)
0