@@ -83,9 +83,9 @@ Framework - works.
83
83
Initially, using the :class: `Symfony\\ Component\\ HttpKernel\\ HttpKernel `
84
84
is really simple and involves creating an
85
85
:doc: `event dispatcher </components/event_dispatcher/introduction >` and a
86
- :ref: `controller resolver <component-http-kernel-resolve-controller >` (explained
87
- below). To complete your working kernel, you'll add more event listeners
88
- to the events discussed below::
86
+ :ref: `controller and argument resolver <component-http-kernel-resolve-controller >`
87
+ (explained below). To complete your working kernel, you'll add more event
88
+ listeners to the events discussed below::
89
89
90
90
use Symfony\Component\HttpFoundation\Request;
91
91
use Symfony\Component\HttpKernel\HttpKernel;
@@ -122,13 +122,12 @@ See ":ref:`http-kernel-working-example`" for a more concrete implementation.
122
122
For general information on adding listeners to the events below, see
123
123
:ref: `http-kernel-creating-listener `.
124
124
125
-
126
125
.. caution ::
127
126
128
- As of 3.1 the :class: `Symfony\\ Component\\ Httpkernel\\ HttpKernel ` accepts a fourth argument, which
129
- must be an instance of :class: ` Symfony \\ Component \\ Httpkernel \\ Controller \\ ArgumentResolverInterface `.
130
- In 4.0 this argument will become mandatory and the :class: `Symfony\\ Component\\ Httpkernel\\ HttpKernel `
131
- will no longer be able to fall back to the :class: ` Symfony \\ Component \\ Httpkernel \\ Controller \\ ControllerResolver ` .
127
+ As of 3.1 the :class: `Symfony\\ Component\\ Httpkernel\\ HttpKernel ` accepts a
128
+ fourth argument, which must be an instance of
129
+ :class: `Symfony\\ Component\\ Httpkernel\\ Controller \\ ArgumentResolverInterface `.
130
+ In 4.0 this argument will become mandatory .
132
131
133
132
.. seealso ::
134
133
@@ -210,7 +209,7 @@ the next step in HttpKernel is to determine and prepare (i.e. resolve) the
210
209
controller. The controller is the part of the end-application's code that
211
210
is responsible for creating and returning the ``Response `` for a specific page.
212
211
The only requirement is that it is a PHP callable - i.e. a function, method
213
- on an object, or a ``Closure ``.
212
+ on an object or a ``Closure ``.
214
213
215
214
But *how * you determine the exact controller for a request is entirely up
216
215
to your application. This is the job of the "controller resolver" - a class
@@ -239,11 +238,14 @@ This implementation is explained more in the sidebar below::
239
238
240
239
.. caution ::
241
240
242
- The ``getArguments() `` method in the :class: `Symfony\\ Component\\ Httpkernel\\ Controller\\ ControllerResolver `
243
- and respective interface :class: `Symfony\\ Component\\ Httpkernel\\ Controller\\ ControllerResolverInterface `
241
+ The ``getArguments() `` method in the
242
+ :class: `Symfony\\ Component\\ Httpkernel\\ Controller\\ ControllerResolver ` and
243
+ respective interface
244
+ :class: `Symfony\\ Component\\ Httpkernel\\ Controller\\ ControllerResolverInterface `
244
245
are deprecated as of 3.1 and will be removed in 4.0. You can use the
245
- :class: `Symfony\\ Component\\ Httpkernel\\ Controller\\ ArgumentResolver ` which uses the
246
- :class: `Symfony\\ Component\\ Httpkernel\\ Controller\\ ArgumentResolverInterface ` instead.
246
+ :class: `Symfony\\ Component\\ Httpkernel\\ Controller\\ ArgumentResolver ` which
247
+ uses the :class: `Symfony\\ Component\\ Httpkernel\\ Controller\\ ArgumentResolverInterface `
248
+ instead.
247
249
248
250
Internally, the ``HttpKernel::handle `` method first calls
249
251
:method: `Symfony\\ Component\\ HttpKernel\\ Controller\\ ControllerResolverInterface::getController `
@@ -330,9 +332,9 @@ on the event object that's passed to listeners on this event.
330
332
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
331
333
332
334
Next, ``HttpKernel::handle `` calls
333
- :method: `Symfony\\ Component\\ HttpKernel\\ Controller\\ ArgumentResolverInterface::getArguments `.
334
- Remember that the controller returned in ``getController `` is a callable.
335
- The purpose of ``getArguments `` is to return the array of arguments that
335
+ :method: `ArgumentResolverInterface::getArguments() < Symfony\\ Component\\ HttpKernel\\ Controller\\ ArgumentResolverInterface::getArguments> `.
336
+ Remember that the controller returned in ``getController() `` is a callable.
337
+ The purpose of ``getArguments() `` is to return the array of arguments that
336
338
should be passed to that controller. Exactly how this is done is completely
337
339
up to your design, though the built-in :class: `Symfony\\ Component\\ HttpKernel\\ Controller\\ ArgumentResolver `
338
340
is a good example.
@@ -353,7 +355,7 @@ of arguments that should be passed when executing that callable.
353
355
354
356
a) If the ``Request `` attributes bag contains a key that matches the name
355
357
of the argument, that value is used. For example, if the first argument
356
- to a controller is ``$slug ``, and there is a ``slug `` key in the ``Request ``
358
+ to a controller is ``$slug `` and there is a ``slug `` key in the ``Request ``
357
359
``attributes `` bag, that value is used (and typically this value came
358
360
from the ``RouterListener ``).
359
361
@@ -363,14 +365,15 @@ of arguments that should be passed when executing that callable.
363
365
class, it will be injected as long as you extend the Symfony ``Request ``.
364
366
365
367
c) If the function or method argument is `variadic `_ and the ``Request ``
366
- ``attributes `` bag contains and array for that argument, they will all be
368
+ ``attributes `` bag contains an array for that argument, they will all be
367
369
available through the `variadic `_ argument.
368
370
369
371
This functionality is provided by resolvers implementing the
370
372
:class: `Symfony\\ Component\\ HttpKernel\\ Controller\\ ArgumentValueResolverInterface `.
371
- There are four implementations which provide the default behavior of Symfony but
372
- customization is the key here. By implementing the ``ArgumentValueResolverInterface ``
373
- yourself and passing this to the ``ArgumentResolver ``, you can extend this functionality.
373
+ There are four implementations which provide the default behavior of
374
+ Symfony but customization is the key here. By implementing the
375
+ ``ArgumentValueResolverInterface `` yourself and passing this to the
376
+ ``ArgumentResolver ``, you can extend this functionality.
374
377
375
378
.. _component-http-kernel-calling-controller :
376
379
@@ -650,45 +653,44 @@ use any argument resolver that implements the
650
653
However, the HttpKernel component comes with some built-in listeners and everything
651
654
else that can be used to create a working example::
652
655
653
- use Symfony\Component\EventDispatcher\EventDispatcher;
654
- use Symfony\Component\HttpFoundation\Request;
655
- use Symfony\Component\HttpFoundation\RequestStack;
656
- use Symfony\Component\HttpFoundation\Response;
657
- use Symfony\Component\HttpKernel\Controller\ArgumentResolver;
658
- use Symfony\Component\HttpKernel\Controller\ControllerResolver;
659
- use Symfony\Component\HttpKernel\EventListener\RouterListener;
660
- use Symfony\Component\HttpKernel\HttpKernel;
661
- use Symfony\Component\Routing\Matcher\UrlMatcher;
662
- use Symfony\Component\Routing\RequestContext;
663
- use Symfony\Component\Routing\Route;
664
- use Symfony\Component\Routing\RouteCollection;
665
-
666
- $routes = new RouteCollection();
667
- $routes->add('hello', new Route('/hello/{name}', array(
668
- '_controller' => function (Request $request) {
669
- return new Response(
670
- sprintf("Hello %s", $request->get('name'))
671
- );
672
- })
673
- ));
674
-
675
- $request = Request::createFromGlobals();
656
+ use Symfony\Component\EventDispatcher\EventDispatcher;
657
+ use Symfony\Component\HttpFoundation\Request;
658
+ use Symfony\Component\HttpFoundation\RequestStack;
659
+ use Symfony\Component\HttpFoundation\Response;
660
+ use Symfony\Component\HttpKernel\Controller\ArgumentResolver;
661
+ use Symfony\Component\HttpKernel\Controller\ControllerResolver;
662
+ use Symfony\Component\HttpKernel\EventListener\RouterListener;
663
+ use Symfony\Component\HttpKernel\HttpKernel;
664
+ use Symfony\Component\Routing\Matcher\UrlMatcher;
665
+ use Symfony\Component\Routing\RequestContext;
666
+ use Symfony\Component\Routing\Route;
667
+ use Symfony\Component\Routing\RouteCollection;
668
+
669
+ $routes = new RouteCollection();
670
+ $routes->add('hello', new Route('/hello/{name}', array(
671
+ '_controller' => function (Request $request) {
672
+ return new Response(
673
+ sprintf("Hello %s", $request->get('name'))
674
+ );
675
+ })
676
+ ));
676
677
677
- $matcher = new UrlMatcher($routes, new RequestContext() );
678
+ $request = Request::createFromGlobals( );
678
679
679
- $dispatcher = new EventDispatcher();
680
- $dispatcher->addSubscriber(new RouterListener($matcher, new RequestStack()));
680
+ $matcher = new UrlMatcher($routes, new RequestContext());
681
681
682
- $controllerResolver = new ControllerResolver ();
683
- $argumentResolver = new ArgumentResolver( );
682
+ $dispatcher = new EventDispatcher ();
683
+ $dispatcher->addSubscriber(new RouterListener($matcher, new RequestStack()) );
684
684
685
- $kernel = new HttpKernel($dispatcher, $controllerResolver, new RequestStack(), $argumentResolver);
685
+ $controllerResolver = new ControllerResolver();
686
+ $argumentResolver = new ArgumentResolver();
686
687
687
- $response = $kernel->handle($request);
688
- $response->send();
688
+ $kernel = new HttpKernel($dispatcher, $controllerResolver, new RequestStack(), $argumentResolver);
689
689
690
- $kernel->terminate($request, $response);
690
+ $response = $kernel->handle($request);
691
+ $response->send();
691
692
693
+ $kernel->terminate($request, $response);
692
694
693
695
.. _http-kernel-sub-requests :
694
696
0 commit comments