Skip to content

Commit c15c2ee

Browse files
committed
Merge pull request symfony#2912 from xabbuh/issue-2910
change forward example to show how to create a sub-request explicitly wi...
2 parents 807971d + 9ae2030 commit c15c2ee

File tree

2 files changed

+18
-10
lines changed

2 files changed

+18
-10
lines changed

book/controller.rst

+16-10
Original file line numberDiff line numberDiff line change
@@ -452,7 +452,8 @@ perform a 301 (permanent) redirect, modify the second argument::
452452
Forwarding
453453
~~~~~~~~~~
454454

455-
You can also easily forward to another controller internally with the ``forward()``
455+
You can also easily forward to another controller internally with the
456+
:method:`Symfony\\Bundle\\FrameworkBundle\\Controller\\Controller::forward`
456457
method. Instead of redirecting the user's browser, it makes an internal sub-request,
457458
and calls the specified controller. The ``forward()`` method returns the ``Response``
458459
object that's returned from that controller::
@@ -492,17 +493,22 @@ value to each variable.
492493

493494
Like other base ``Controller`` methods, the ``forward`` method is just
494495
a shortcut for core Symfony2 functionality. A forward can be accomplished
495-
directly via the ``http_kernel`` service and returns a ``Response``
496-
object::
496+
directly by duplicating the current request. When this
497+
:ref:`sub request<http-kernel-sub-requests>` is executed via the ``http_kernel``
498+
service the ``HttpKernel`` returns a ``Response`` object::
499+
500+
use Symfony\Component\HttpKernel/HttpKernelInterface;
501+
502+
$path = array(
503+
'_controller' => 'AcmeHelloBundle:Hello:fancy',
504+
'name' => $name,
505+
'color' => 'green',
506+
);
507+
$request = $this->container->get('request');
508+
$subRequest = $request->duplicate(array(), null, $path);
497509

498510
$httpKernel = $this->container->get('http_kernel');
499-
$response = $httpKernel->forward(
500-
'AcmeHelloBundle:Hello:fancy',
501-
array(
502-
'name' => $name,
503-
'color' => 'green',
504-
)
505-
);
511+
$response = $httpKernel->handle($subRequest, HttpKernelInterface::SUB_REQUEST);
506512

507513
.. index::
508514
single: Controller; Rendering templates

components/http_kernel/introduction.rst

+2
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)