Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit678c705

Browse files
author
Iltar van der Berg
committed
Documented the ArgumentResolver along the ControllerResolver
1 parentd7724dd commit678c705

File tree

1 file changed

+52
-19
lines changed

1 file changed

+52
-19
lines changed

‎create_framework/http_kernel_controller_resolver.rst‎

Lines changed: 52 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -43,10 +43,20 @@ 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.
58+
59+
..code-block::php
5060
5161
namespace Symfony\Component\HttpKernel\Controller;
5262
@@ -58,6 +68,12 @@ a Request object. All controller resolvers implement the following interface::
5868
function getArguments(Request $request, $controller);
5969
}
6070
71+
// ...
72+
interface ArgumentResolverInterface
73+
{
74+
function getArguments(Request $request, $controller);
75+
}
76+
6177
The ``getController()`` method relies on the same convention as the one we
6278
have defined earlier: the ``_controller`` request attribute must contain the
6379
controller associated with the Request. Besides the built-in PHP callbacks,
@@ -74,10 +90,14 @@ resolver from HttpKernel::
7490

7591
use Symfony\Component\HttpKernel;
7692

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

79-
$controller = $resolver->getController($request);
80-
$arguments = $resolver->getArguments($request, $controller);
99+
$controller = $controllerResolver->getController($request);
100+
$arguments = $argumentResolver->getArguments($request, $controller);
81101

82102
$response = call_user_func_array($controller, $arguments);
83103

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

141161
..note::
142162

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.
163+
With the great flexibility of the default controller resolver and argument
164+
resolver, you might wonder why someone would want to create another one
165+
(why would there be an interface if not?). Two examples: in Symfony,
166+
``getController()`` is enhanced to support:doc:`controllers as services</cookbook/controller/service>`;
167+
and ``getArguments()`` provides an extension point to alter or enhance
168+
the resolving of arguments.
151169

152170
Let's conclude with the new version of our framework::
153171

154172
// example.com/web/front.php
155173
require_once __DIR__.'/../vendor/autoload.php';
156174

175+
use Symfony\Component\HttpKernel\Controller\ArgumentValueResolver as ArgumentValueResolver;
157176
use Symfony\Component\HttpFoundation\Request;
158177
use Symfony\Component\HttpFoundation\Response;
159178
use Symfony\Component\Routing;
@@ -174,13 +193,27 @@ Let's conclude with the new version of our framework::
174193
$context = new Routing\RequestContext();
175194
$context->fromRequest($request);
176195
$matcher = new Routing\Matcher\UrlMatcher($routes, $context);
177-
$resolver = new HttpKernel\Controller\ControllerResolver();
196+
197+
$valueResolvers = [
198+
new ArgumentValueResolver\ArgumentFromAttributeResolver(),
199+
new ArgumentValueResolver\VariadicArgumentValueResolver(),
200+
new ArgumentValueResolver\RequestResolver(),
201+
new ArgumentValueResolver\DefaultArgumentValueResolver(),
202+
];
203+
204+
$argumentMetadataFactory = new Symfony\Component\HttpKernel\ControllerMetadata\ArgumentMetadataFactory();
205+
206+
$controllerResolver = new HttpKernel\Controller\ControllerResolver();
207+
$argumentResolver = new HttpKernel\Controller\ArgumentResolver($argumentMetadataFactory, $valueResolvers);
208+
209+
$controller = $controllerResolver->getController($request);
210+
$arguments = $argumentResolver->getArguments($request, $controller);
178211

179212
try {
180213
$request->attributes->add($matcher->match($request->getPathInfo()));
181214

182-
$controller = $resolver->getController($request);
183-
$arguments = $resolver->getArguments($request, $controller);
215+
$controller = $controllerResolver->getController($request);
216+
$arguments = $argumentResolver->getArguments($request, $controller);
184217

185218
$response = call_user_func_array($controller, $arguments);
186219
} catch (Routing\Exception\ResourceNotFoundException $e) {
@@ -192,7 +225,7 @@ Let's conclude with the new version of our framework::
192225
$response->send();
193226

194227
Think about it once more: our framework is more robust and more flexible than
195-
ever and it still has less than40 lines of code.
228+
ever and it still has less than60 lines of code.
196229

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

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp