Enable more complex redirect rules.
Attach it on the fly in your controller beforeFilter, this is recommended if you want to attach it only to specific controllers and actions:
<?php
class SamplesController extends AppController
{
public function beforeFilter(\Cake\Event\EventInterface $event)
{
$this->Crud->addListener('Crud.Redirect');
parent::beforeFilter($event);
}
}
Attach it using components array, this is recommended if you want to attach it to all controllers, application wide:
<?php
class SamplesController extends AppController {
public function initialize()
{
$this->loadComponent('Crud.Crud', [
'actions' => [
'index',
'view'
],
'listeners' => [
'Crud.Redirect'
]
]);
}
}
A reader is a closure that can access a field in an object through different means.
Below is a list of the build-in readers you can use:
Name |
Pseudo code |
Description |
---|---|---|
|
|
Access a property directly on the Request object |
|
|
Access a HTTP POST data field using |
|
|
Access a HTTP query argument using |
|
|
Access a property directly on the Model instance |
|
|
Access a model data key using |
|
|
Access a model key by going to the database and read the value |
|
|
Access a property directly on the event subject |
Adding or overriding a reader is very simple.
The closure takes two arguments:
CrudSubject $subject
$key = null
<?php
class SamplesController extends AppController
{
public function beforeFilter(\Cake\Event\EventInterface $event)
{
$listener = $this->Crud->listener('Redirect');
$listener->reader($name, Closure $closure);
// Example on a reader using Configure
$listener->reader('configure.key', function(CrudSubject $subject, $key) {
return Configure::read($key);
});
parent::beforeFilter();
}
}
?>
Below is the defaults provided by build-in Crud actions:
By default Add Crud Action always redirect to array('action' => 'index')
on afterSave
Name |
Reader |
Key |
Result |
Description |
---|---|---|---|---|
|
|
|
|
By providing |
|
|
|
|
By providing |
By default Edit Crud Action always redirect to array('action' => 'index')
on afterSave
Name |
Reader |
Key |
Result |
Description |
---|---|---|---|---|
|
|
|
|
By providing |
|
|
|
|
By providing |
It’s very simple to modify existing or add your own redirect rules:
<?php
class SamplesController extends AppController
{
public function beforeFilter(\Cake\Event\EventInterface $event)
{
// Get all the redirect rules
$rules = $this->Crud->action()->redirectConfig();
// Get one named rule only
$rule = $this->Crud->action()->redirectConfig('add');
// Configure a redirect rule:
//
// if $_POST['_view'] is set then redirect to
// 'view' action with the value of '$subject->id'
$this->Crud->action()->redirectConfig('view',
[
'reader' => 'request.data', // Any reader from the list above
'key' => '_view', // The key to check for, passed to the reader
'url' => [ // The url to redirect to
'action' => 'view', // The final url will be '/view/$id'
['subject.key', 'id'] // If an array is encountered, it will be expanded the same was as 'reader'+'key'
]
]
);
parent::beforeFilter($event);
}
}