Skip to content

Commit e8511cb

Browse files
committed
feature #3776 Updated event_listener.rst (bfgasparin)
This PR was merged into the 2.4 branch. Discussion ---------- Updated event_listener.rst Updated event_listener.rst to use the new KernelEvent::isMasterRequest method | Q | A | ------------- | --- | Doc fix? | yes | New docs? | no | Applies to | 2.4+ | Fixed tickets | none Commits ------- 4fc3b6a Fixed internals.rst syntax a6b17a0 Added KernelEvent::isMasterRequest method reference 8e8b8a0 Added Versionadded directive to event_listener.rst 0127bd2 Updated event_listener.rst
2 parents fc2698b + 4fc3b6a commit e8511cb

File tree

3 files changed

+23
-8
lines changed

3 files changed

+23
-8
lines changed

book/internals.rst

+11-4
Original file line numberDiff line numberDiff line change
@@ -208,6 +208,10 @@ processing must only occur on the master request).
208208
Events
209209
~~~~~~
210210

211+
.. versionadded:: 2.4
212+
The ``isMasterRequest()`` method was introduced in Symfony 2.4.
213+
Prior, the ``getRequestType()`` method must be used.
214+
211215
Each event thrown by the Kernel is a subclass of
212216
:class:`Symfony\\Component\\HttpKernel\\Event\\KernelEvent`. This means that
213217
each event has access to the same basic information:
@@ -216,22 +220,25 @@ each event has access to the same basic information:
216220
- returns the *type* of the request (``HttpKernelInterface::MASTER_REQUEST``
217221
or ``HttpKernelInterface::SUB_REQUEST``);
218222

223+
* :method:`Symfony\\Component\\HttpKernel\\Event\\KernelEvent::isMasterRequest`
224+
- checks if it is a master request;
225+
219226
* :method:`Symfony\\Component\\HttpKernel\\Event\\KernelEvent::getKernel`
220227
- returns the Kernel handling the request;
221228

222229
* :method:`Symfony\\Component\\HttpKernel\\Event\\KernelEvent::getRequest`
223230
- returns the current ``Request`` being handled.
224231

225-
``getRequestType()``
226-
....................
232+
``isMasterRequest()``
233+
.....................
227234

228-
The ``getRequestType()`` method allows listeners to know the type of the
235+
The ``isMasterRequest()`` method allows listeners to check the type of the
229236
request. For instance, if a listener must only be active for master requests,
230237
add the following code at the beginning of your listener method::
231238

232239
use Symfony\Component\HttpKernel\HttpKernelInterface;
233240

234-
if (HttpKernelInterface::MASTER_REQUEST !== $event->getRequestType()) {
241+
if (!$event->isMasterRequest()) {
235242
// return immediately
236243
return;
237244
}

components/http_kernel/introduction.rst

+7-3
Original file line numberDiff line numberDiff line change
@@ -665,12 +665,16 @@ argument as follows::
665665
$response = $kernel->handle($request, HttpKernelInterface::SUB_REQUEST);
666666
// do something with this response
667667

668+
.. versionadded:: 2.4
669+
The ``isMasterRequest()`` method was introduced in Symfony 2.4.
670+
Prior, the ``getRequestType()`` method must be used.
671+
668672
This creates another full request-response cycle where this new ``Request`` is
669673
transformed into a ``Response``. The only difference internally is that some
670674
listeners (e.g. security) may only act upon the master request. Each listener
671675
is passed some sub-class of :class:`Symfony\\Component\\HttpKernel\\Event\\KernelEvent`,
672-
whose :method:`Symfony\\Component\\HttpKernel\\Event\\KernelEvent::getRequestType`
673-
can be used to figure out if the current request is a "master" or "sub" request.
676+
whose :method:`Symfony\\Component\\HttpKernel\\Event\\KernelEvent::isMasterRequest`
677+
can be used to check if the current request is a "master" or "sub" request.
674678

675679
For example, a listener that only needs to act on the master request may
676680
look like this::
@@ -680,7 +684,7 @@ look like this::
680684

681685
public function onKernelRequest(GetResponseEvent $event)
682686
{
683-
if (HttpKernelInterface::MASTER_REQUEST !== $event->getRequestType()) {
687+
if (!$event->isMasterRequest()) {
684688
return;
685689
}
686690

cookbook/service_container/event_listener.rst

+5-1
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,10 @@ using a special "tag":
100100
Request events, checking types
101101
------------------------------
102102

103+
.. versionadded:: 2.4
104+
The ``isMasterRequest()`` method was introduced in Symfony 2.4.
105+
Prior, the ``getRequestType()`` method must be used.
106+
103107
A single page can make several requests (one master request, and then multiple
104108
sub-requests), which is why when working with the ``KernelEvents::REQUEST``
105109
event, you might need to check the type of the request. This can be easily
@@ -115,7 +119,7 @@ done as follow::
115119
{
116120
public function onKernelRequest(GetResponseEvent $event)
117121
{
118-
if (HttpKernel::MASTER_REQUEST != $event->getRequestType()) {
122+
if (!$event->isMasterRequest()) {
119123
// don't do anything if it's not the master request
120124
return;
121125
}

0 commit comments

Comments
 (0)