Skip to content

Commit 52cce3d

Browse files
authored
Merge pull request #8045 from cakephp/5.x-event
Document 5.x redirecting and add void return types.
2 parents b85e490 + eaf5ed7 commit 52cce3d

File tree

16 files changed

+37
-33
lines changed

16 files changed

+37
-33
lines changed

en/controllers.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -564,7 +564,7 @@ Remember to call ``AppController``'s callbacks within child controller callbacks
564564
for best results::
565565

566566
//use Cake\Event\EventInterface;
567-
public function beforeFilter(EventInterface $event)
567+
public function beforeFilter(EventInterface $event): void
568568
{
569569
parent::beforeFilter($event);
570570
}

en/controllers/components.rst

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ You can configure components at runtime using the ``setConfig()`` method. Often,
4545
this is done in your controller's ``beforeFilter()`` method. The above could
4646
also be expressed as::
4747

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

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

338-
public function beforeFilter(EventInterface $event)
338+
public function beforeFilter(EventInterface $event): void
339339
{
340-
$event->stopPropagation();
340+
if (...) {
341+
$event->setResult($this->getController()->redirect('/'));
341342

342-
return $this->getController()->redirect('/');
343+
return;
344+
}
345+
346+
...
343347
}
344348

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

353-
public function beforeFilter(EventInterface $event)
357+
public function beforeFilter(EventInterface $event): void
354358
{
355359
throw new RedirectException(Router::url('/'))
356360
}

en/controllers/components/form-protection.rst

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ Disabling form tampering checks
9393
$this->loadComponent('FormProtection');
9494
}
9595

96-
public function beforeFilter(EventInterface $event)
96+
public function beforeFilter(EventInterface $event): void
9797
{
9898
parent::beforeFilter($event);
9999

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

129-
public function beforeFilter(EventInterface $event)
129+
public function beforeFilter(EventInterface $event): void
130130
{
131131
parent::beforeFilter($event);
132132

@@ -148,7 +148,7 @@ works::
148148

149149
use Cake\Controller\Exception\FormProtectionException;
150150

151-
public function beforeFilter(EventInterface $event)
151+
public function beforeFilter(EventInterface $event): void
152152
{
153153
parent::beforeFilter($event);
154154

en/core-libraries/email.rst

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -379,14 +379,14 @@ application's code, we can have our ``UserMailer`` subscribe to the
379379
application's user-related classes completely free of email-related logic and
380380
instructions. For example, we could add the following to our ``UserMailer``::
381381

382-
public function implementedEvents()
382+
public function implementedEvents(): array
383383
{
384384
return [
385385
'Model.afterSave' => 'onRegistration',
386386
];
387387
}
388388

389-
public function onRegistration(EventInterface $event, EntityInterface $entity, ArrayObject $options)
389+
public function onRegistration(EventInterface $event, EntityInterface $entity, ArrayObject $options): void
390390
{
391391
if ($entity->isNew()) {
392392
$this->send('welcome', [$entity]);

en/core-libraries/events.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -375,7 +375,7 @@ for event listeners::
375375
// Setting priority for a listener
376376
class UserStatistic implements EventListenerInterface
377377
{
378-
public function implementedEvents()
378+
public function implementedEvents(): array
379379
{
380380
return [
381381
'Order.afterPlace' => [

en/development/errors.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -201,7 +201,7 @@ prefix. You could create the following class::
201201
* @param \Cake\Event\EventInterface $event Event.
202202
* @return void
203203
*/
204-
public function beforeRender(EventInterface $event)
204+
public function beforeRender(EventInterface $event): void
205205
{
206206
$this->viewBuilder()->setTemplatePath('Error');
207207
}

en/development/testing.rst

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1688,12 +1688,12 @@ controllers that use it. Here is our example component located in
16881688
}
16891689
}
16901690

1691-
public function startup(EventInterface $event)
1691+
public function startup(EventInterface $event): void
16921692
{
16931693
$this->setController($event->getSubject());
16941694
}
16951695

1696-
public function adjust($length = 'short'): void
1696+
public function adjust(string $length = 'short'): void
16971697
{
16981698
switch ($length) {
16991699
case 'long':

en/orm/behaviors.rst

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,7 @@ behavior should now look like::
168168
$entity->set($config['slug'], Text::slug($value, $config['replacement']));
169169
}
170170

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

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

187-
public function beforeSave(EventInterface $event, EntityInterface $entity, ArrayObject $options)
187+
public function beforeSave(EventInterface $event, EntityInterface $entity, ArrayObject $options): void
188188
{
189189
if (...) {
190190
$event->stopPropagation();

en/orm/saving-data.rst

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -595,7 +595,7 @@ request data just before entities are created::
595595
use ArrayObject;
596596

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

622622
// In a table or behavior class
623-
public function beforeMarshal(EventInterface $event, ArrayObject $data, ArrayObject $options)
623+
public function beforeMarshal(EventInterface $event, ArrayObject $data, ArrayObject $options): void
624624
{
625625
foreach ($data as $key => $value) {
626626
if (is_string($value)) {
@@ -656,7 +656,7 @@ validation logic that you cannot easily express through Validator methods::
656656
EntityInterface $entity,
657657
ArrayObject $data,
658658
ArrayObject $options
659-
) {
659+
): void {
660660
// Don't accept people who have a name starting with J on the 20th
661661
// of each month.
662662
if (mb_substr($entity->name, 1) === 'J' && (int)date('d') === 20) {

en/orm/table-objects.rst

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,7 @@ which implements ``EventListenerInterface``::
183183
use Cake\Event\EventListenerInterface;
184184
class ModelInitializeListener implements EventListenerInterface
185185
{
186-
public function implementedEvents()
186+
public function implementedEvents(): array
187187
{
188188
return [
189189
'Model.initialize' => 'initializeEvent',
@@ -231,7 +231,7 @@ The ``Model.beforeFind`` event is fired before each find operation. By stopping
231231
the event, and feeding the query with a custom result set, you can bypass the find
232232
operation entirely::
233233

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

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

396396
// In a Table class.
397-
public function implementedEvents()
397+
public function implementedEvents(): array
398398
{
399399
$events = parent::implementedEvents();
400400
$events['Model.beforeDelete'] = [

0 commit comments

Comments
 (0)