Skip to content

Commit 8c7576d

Browse files
authored
Merge pull request #8046 from cakephp/5.x-event-2
Document 5.x redirecting
2 parents 52cce3d + b9bc87a commit 8c7576d

File tree

2 files changed

+23
-1
lines changed

2 files changed

+23
-1
lines changed

en/controllers.rst

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -571,6 +571,28 @@ for best results::
571571

572572
.. _controller-middleware:
573573

574+
Using Redirects in Controller Events
575+
====================================
576+
577+
To redirect from within a controller callback method you can use the following::
578+
579+
public function beforeFilter(EventInterface $event): void
580+
{
581+
if (...) {
582+
$event->setResult($this->redirect('/'));
583+
584+
return;
585+
}
586+
587+
...
588+
}
589+
590+
By setting a redirect as event result you let CakePHP know that you don't want any other
591+
component callbacks to run, and that the controller should not handle the action
592+
any further.
593+
594+
As of 4.1.0 you can also raise a ``RedirectException`` to signal a redirect.
595+
574596
Controller Middleware
575597
=====================
576598

en/controllers/components.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -346,7 +346,7 @@ To redirect from within a component callback method you can use the following::
346346
...
347347
}
348348

349-
By stopping the event you let CakePHP know that you don't want any other
349+
By setting a redirect as event result you let CakePHP know that you don't want any other
350350
component callbacks to run, and that the controller should not handle the action
351351
any further. As of 4.1.0 you can raise a ``RedirectException`` to signal
352352
a redirect::

0 commit comments

Comments
 (0)