Skip to content

Document 5.x redirecting and add void return types. #8045

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 23, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion en/controllers.rst
Original file line number Diff line number Diff line change
Expand Up @@ -564,7 +564,7 @@ Remember to call ``AppController``'s callbacks within child controller callbacks
for best results::

//use Cake\Event\EventInterface;
public function beforeFilter(EventInterface $event)
public function beforeFilter(EventInterface $event): void
{
parent::beforeFilter($event);
}
Expand Down
14 changes: 9 additions & 5 deletions en/controllers/components.rst
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ You can configure components at runtime using the ``setConfig()`` method. Often,
this is done in your controller's ``beforeFilter()`` method. The above could
also be expressed as::

public function beforeFilter(EventInterface $event)
public function beforeFilter(EventInterface $event): void
{
$this->FormProtection->setConfig('unlockedActions', ['index']);
}
Expand Down Expand Up @@ -335,11 +335,15 @@ Using Redirects in Component Events

To redirect from within a component callback method you can use the following::

public function beforeFilter(EventInterface $event)
public function beforeFilter(EventInterface $event): void
{
$event->stopPropagation();
if (...) {
$event->setResult($this->getController()->redirect('/'));

return $this->getController()->redirect('/');
return;
}

...
}

By stopping the event you let CakePHP know that you don't want any other
Expand All @@ -350,7 +354,7 @@ a redirect::
use Cake\Http\Exception\RedirectException;
use Cake\Routing\Router;

public function beforeFilter(EventInterface $event)
public function beforeFilter(EventInterface $event): void
{
throw new RedirectException(Router::url('/'))
}
Expand Down
6 changes: 3 additions & 3 deletions en/controllers/components/form-protection.rst
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ Disabling form tampering checks
$this->loadComponent('FormProtection');
}

public function beforeFilter(EventInterface $event)
public function beforeFilter(EventInterface $event): void
{
parent::beforeFilter($event);

Expand Down Expand Up @@ -126,7 +126,7 @@ action (ex. AJAX requests). You may "unlock" these actions by listing them in
$this->loadComponent('FormProtection');
}

public function beforeFilter(EventInterface $event)
public function beforeFilter(EventInterface $event): void
{
parent::beforeFilter($event);

Expand All @@ -148,7 +148,7 @@ works::

use Cake\Controller\Exception\FormProtectionException;

public function beforeFilter(EventInterface $event)
public function beforeFilter(EventInterface $event): void
{
parent::beforeFilter($event);

Expand Down
4 changes: 2 additions & 2 deletions en/core-libraries/email.rst
Original file line number Diff line number Diff line change
Expand Up @@ -379,14 +379,14 @@ application's code, we can have our ``UserMailer`` subscribe to the
application's user-related classes completely free of email-related logic and
instructions. For example, we could add the following to our ``UserMailer``::

public function implementedEvents()
public function implementedEvents(): array
{
return [
'Model.afterSave' => 'onRegistration',
];
}

public function onRegistration(EventInterface $event, EntityInterface $entity, ArrayObject $options)
public function onRegistration(EventInterface $event, EntityInterface $entity, ArrayObject $options): void
{
if ($entity->isNew()) {
$this->send('welcome', [$entity]);
Expand Down
2 changes: 1 addition & 1 deletion en/core-libraries/events.rst
Original file line number Diff line number Diff line change
Expand Up @@ -375,7 +375,7 @@ for event listeners::
// Setting priority for a listener
class UserStatistic implements EventListenerInterface
{
public function implementedEvents()
public function implementedEvents(): array
{
return [
'Order.afterPlace' => [
Expand Down
2 changes: 1 addition & 1 deletion en/development/errors.rst
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,7 @@ prefix. You could create the following class::
* @param \Cake\Event\EventInterface $event Event.
* @return void
*/
public function beforeRender(EventInterface $event)
public function beforeRender(EventInterface $event): void
{
$this->viewBuilder()->setTemplatePath('Error');
}
Expand Down
4 changes: 2 additions & 2 deletions en/development/testing.rst
Original file line number Diff line number Diff line change
Expand Up @@ -1688,12 +1688,12 @@ controllers that use it. Here is our example component located in
}
}

public function startup(EventInterface $event)
public function startup(EventInterface $event): void
{
$this->setController($event->getSubject());
}

public function adjust($length = 'short'): void
public function adjust(string $length = 'short'): void
{
switch ($length) {
case 'long':
Expand Down
4 changes: 2 additions & 2 deletions en/orm/behaviors.rst
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ behavior should now look like::
$entity->set($config['slug'], Text::slug($value, $config['replacement']));
}

public function beforeSave(EventInterface $event, EntityInterface $entity, ArrayObject $options)
public function beforeSave(EventInterface $event, EntityInterface $entity, ArrayObject $options): void
{
$this->slug($entity);
}
Expand All @@ -184,7 +184,7 @@ The above code shows a few interesting features of behaviors:

To prevent the save from continuing, simply stop event propagation in your callback::

public function beforeSave(EventInterface $event, EntityInterface $entity, ArrayObject $options)
public function beforeSave(EventInterface $event, EntityInterface $entity, ArrayObject $options): void
{
if (...) {
$event->stopPropagation();
Expand Down
6 changes: 3 additions & 3 deletions en/orm/saving-data.rst
Original file line number Diff line number Diff line change
Expand Up @@ -595,7 +595,7 @@ request data just before entities are created::
use ArrayObject;

// In a table or behavior class
public function beforeMarshal(EventInterface $event, ArrayObject $data, ArrayObject $options)
public function beforeMarshal(EventInterface $event, ArrayObject $data, ArrayObject $options): void
{
if (isset($data['username'])) {
$data['username'] = mb_strtolower($data['username']);
Expand All @@ -620,7 +620,7 @@ changing the data before it is validated is trimming all fields before saving::
use ArrayObject;

// In a table or behavior class
public function beforeMarshal(EventInterface $event, ArrayObject $data, ArrayObject $options)
public function beforeMarshal(EventInterface $event, ArrayObject $data, ArrayObject $options): void
{
foreach ($data as $key => $value) {
if (is_string($value)) {
Expand Down Expand Up @@ -656,7 +656,7 @@ validation logic that you cannot easily express through Validator methods::
EntityInterface $entity,
ArrayObject $data,
ArrayObject $options
) {
): void {
// Don't accept people who have a name starting with J on the 20th
// of each month.
if (mb_substr($entity->name, 1) === 'J' && (int)date('d') === 20) {
Expand Down
8 changes: 4 additions & 4 deletions en/orm/table-objects.rst
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ which implements ``EventListenerInterface``::
use Cake\Event\EventListenerInterface;
class ModelInitializeListener implements EventListenerInterface
{
public function implementedEvents()
public function implementedEvents(): array
{
return [
'Model.initialize' => 'initializeEvent',
Expand Down Expand Up @@ -231,7 +231,7 @@ The ``Model.beforeFind`` event is fired before each find operation. By stopping
the event, and feeding the query with a custom result set, you can bypass the find
operation entirely::

public function beforeFind(EventInterface $event, SelectQuery $query, ArrayObject $options, $primary)
public function beforeFind(EventInterface $event, SelectQuery $query, ArrayObject $options, $primary): void
{
if (/* ... */) {
$event->stopPropagation();
Expand Down Expand Up @@ -350,7 +350,7 @@ Stopping Table Events
---------------------
To prevent the save from continuing, simply stop event propagation in your callback::

public function beforeSave(EventInterface $event, EntityInterface $entity, ArrayObject $options)
public function beforeSave(EventInterface $event, EntityInterface $entity, ArrayObject $options): void
{
if (...) {
$event->stopPropagation();
Expand Down Expand Up @@ -394,7 +394,7 @@ You can manage event priorities in one of a few ways:
priority per callback-function::

// In a Table class.
public function implementedEvents()
public function implementedEvents(): array
{
$events = parent::implementedEvents();
$events['Model.beforeDelete'] = [
Expand Down
2 changes: 1 addition & 1 deletion en/tutorials-and-examples/cms/articles-controller.rst
Original file line number Diff line number Diff line change
Expand Up @@ -316,7 +316,7 @@ typically a URL-safe version of an article's title. We can use the

// Add the following method.

public function beforeSave(EventInterface $event, $entity, $options)
public function beforeSave(EventInterface $event, $entity, $options): void
{
if ($entity->isNew() && !$entity->slug) {
$sluggedTitle = Text::slug($entity->title);
Expand Down
4 changes: 2 additions & 2 deletions en/tutorials-and-examples/cms/authentication.rst
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,7 @@ If you visit your site, you'll get an "infinite redirect loop" so let's fix that

In your ``UsersController``, add the following code::

public function beforeFilter(\Cake\Event\EventInterface $event)
public function beforeFilter(\Cake\Event\EventInterface $event): void
{
parent::beforeFilter($event);
// Configure the login action to not require authentication, preventing
Expand Down Expand Up @@ -258,7 +258,7 @@ We want all ``view`` and ``index`` pages accessible without logging in so we'll
configuration in AppController::

// in src/Controller/AppController.php
public function beforeFilter(\Cake\Event\EventInterface $event)
public function beforeFilter(\Cake\Event\EventInterface $event): void
{
parent::beforeFilter($event);
// for all controllers in our application, make index and view
Expand Down
2 changes: 1 addition & 1 deletion en/tutorials-and-examples/cms/tags-and-users.rst
Original file line number Diff line number Diff line change
Expand Up @@ -402,7 +402,7 @@ data from the request into our entity. We can use a ``beforeSave()`` hook method
to parse the tag string and find/build the related entities. Add the following
to **src/Model/Table/ArticlesTable.php**::

public function beforeSave(EventInterface $event, $entity, $options)
public function beforeSave(EventInterface $event, $entity, $options): void
{
if ($entity->tag_string) {
$entity->tags = $this->_buildTags($entity->tag_string);
Expand Down
4 changes: 2 additions & 2 deletions en/views/helpers.rst
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ You can also use your controller's ``beforeRender`` method to add helpers::

class ArticlesController extends AppController
{
public function beforeRender(EventInterface $event)
public function beforeRender(EventInterface $event): void
{
parent::beforeRender($event);
$this->viewBuilder()->addHelper('MyHelper');
Expand Down Expand Up @@ -143,7 +143,7 @@ you can set those in your controller's beforeRender callback::

class PostsController extends AppController
{
public function beforeRender(EventInterface $event)
public function beforeRender(EventInterface $event): void
{
parent::beforeRender($event);
$builder = $this->viewBuilder();
Expand Down
2 changes: 1 addition & 1 deletion en/views/helpers/form.rst
Original file line number Diff line number Diff line change
Expand Up @@ -1567,7 +1567,7 @@ To prevent the ``submittedfile`` from being over-written as blank, remove it
from ``$_accessible``. Alternatively, you can unset the index by using
``beforeMarshal``::

public function beforeMarshal(\Cake\Event\EventInterface $event, \ArrayObject $data, \ArrayObject $options)
public function beforeMarshal(\Cake\Event\EventInterface $event, \ArrayObject $data, \ArrayObject $options): void
{
if ($data['submittedfile'] === '') {
unset($data['submittedfile']);
Expand Down
4 changes: 2 additions & 2 deletions en/views/themes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ To use themes, set the theme name in your controller's action or

class ExamplesController extends AppController
{
public function beforeRender(\Cake\Event\EventInterface $event)
public function beforeRender(\Cake\Event\EventInterface $event): void
{
$this->viewBuilder()->setTheme('Modern');
}
Expand Down Expand Up @@ -50,7 +50,7 @@ Because themes are standard CakePHP plugins, they can include any necessary
assets in their webroot directory. This allows for packaging and
distribution of themes. Whilst in development, requests for theme assets will be
handled by :php:class:`Cake\Routing\Middleware\AssetMiddleware` (which is loaded
by default in cakephp/app ``Application::middleware()``). To improve
by default in cakephp/app ``Application::middleware()``). To improve
performance for production environments, it's recommended that you :ref:`symlink-assets`.

All of CakePHP's built-in helpers are aware of themes and will create the
Expand Down
Loading