Skip to content

Commit 9ae2030

Browse files
committed
change forward example to show how to create a sub-request explicitly without using the deprecated format() method of the HttpKernel class
1 parent e203c40 commit 9ae2030

File tree

2 files changed

+18
-10
lines changed

2 files changed

+18
-10
lines changed

book/controller.rst

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -438,7 +438,8 @@ perform a 301 (permanent) redirect, modify the second argument::
438438
Forwarding
439439
~~~~~~~~~~
440440

441-
You can also easily forward to another controller internally with the ``forward()``
441+
You can also easily forward to another controller internally with the
442+
:method:`Symfony\\Bundle\\FrameworkBundle\\Controller\\Controller::forward`
442443
method. Instead of redirecting the user's browser, it makes an internal sub-request,
443444
and calls the specified controller. The ``forward()`` method returns the ``Response``
444445
object that's returned from that controller::
@@ -478,17 +479,22 @@ value to each variable.
478479

479480
Like other base ``Controller`` methods, the ``forward`` method is just
480481
a shortcut for core Symfony2 functionality. A forward can be accomplished
481-
directly via the ``http_kernel`` service and returns a ``Response``
482-
object::
482+
directly by duplicating the current request. When this
483+
:ref:`sub request<http-kernel-sub-requests>` is executed via the ``http_kernel``
484+
service the ``HttpKernel`` returns a ``Response`` object::
485+
486+
use Symfony\Component\HttpKernel/HttpKernelInterface;
487+
488+
$path = array(
489+
'_controller' => 'AcmeHelloBundle:Hello:fancy',
490+
'name' => $name,
491+
'color' => 'green',
492+
);
493+
$request = $this->container->get('request');
494+
$subRequest = $request->duplicate(array(), null, $path);
483495

484496
$httpKernel = $this->container->get('http_kernel');
485-
$response = $httpKernel->forward(
486-
'AcmeHelloBundle:Hello:fancy',
487-
array(
488-
'name' => $name,
489-
'color' => 'green',
490-
)
491-
);
497+
$response = $httpKernel->handle($subRequest, HttpKernelInterface::SUB_REQUEST);
492498

493499
.. index::
494500
single: Controller; Rendering templates

components/http_kernel/introduction.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -632,6 +632,8 @@ a built-in ControllerResolver that can be used to create a working example::
632632

633633
$kernel->terminate($request, $response);
634634

635+
.. _http-kernel-sub-requests:
636+
635637
Sub Requests
636638
------------
637639

0 commit comments

Comments
 (0)