The Index Crud Action
paginates over the primary model in the controller.
On a high level it’s basically just calling Controller::paginate()
.
Note
Before applying any configuration to an action
it must be mapped first.
If the action has not been mapped an exception will be raised.
Test or modify if the Crud Action is enabled or not.
When a CrudAction is disabled, Crud will not handle any requests to the action, and CakePHP will raise the normal
\Cake\Error\MissingActionException
exception if you haven’t implemented the action in your controller.
Warning
If you have enabled Crud and you are still receiving a MissingActionException
, ensure the action is enabled and
that the controller has the \Crud\Controller\ControllerTrait
implemented.
To test if an action is enabled, call the enabled
method on the action.
$this->Crud->action()->enabled();
To disable an action, call the disable
method on the action.
$this->Crud->action()->disable();
To enable an action, call the enable
method on the action.
$this->Crud->action()->enable();
To disable or enable multiple actions at the same time, Crud Component
provides helper methods.
The enable
and disable
method can take a string or an array, for easy mass-updating.
$this->Crud->enable('index');
$this->Crud->enable(['index', 'add']);
$this->Crud->disable('index');
$this->Crud->disable(['index', 'add']);
Note
These methods simply calls the enable
and disable
method in each Crud Action
class, and do not provide any magic
other than mass updating.
Warning
While it’s possible to update the enabled
property directly on an action using the config
methods,
it’s not recommend, as important cleanup logic will not be applied if you use the setConfig()
method directly.
The 1st parameter to Table::find()
- the default value is all
.
To get the current configured findMethod
keys call the findMethod
method without any arguments.
$this->Crud->action()->findMethod();
To change the findMethod value pass a string argument to the method
$this->Crud->action()->findMethod('my_custom_finder');
Get or set the view file to render at the end of the request.
The view setting is passed directly and unmodified to Controller::render()
.
To get the current configured view
call the view
method without any arguments.
$this->Crud->action()->view();
To change the view to render, pass a string
as first argument.
$this->Crud->action()->view('my_custom_view');
Note
If the first parameter is NULL
- which is the default value - the normal CakePHP behavior will be used.
Warning
Due to the nature of this method, once a custom view has been set, it’s not possible to revert back to
the default behavior by calling ->view(null)
as it will return the current configuration.
Note
This maps directly to the $key
argument in Controller::set($key, $value)
Change the name of the variable which contains the result of a index
or view
action query result.
To get the current configured viewVar
call the viewViar
method without any arguments.
$this->Crud->action()->viewVar();
To change the viewVar, pass a string
as first argument.
$this->Crud->action()->viewVar('items');
For Index Action the default is plural version of the controller name.
Having a controller named PostsController
would mean that the viewVar
would be posts
by default.
For View Action the default is singular version of the controller name.
Having a controller named PostsController
would mean that the viewVar
would be post
by default.
Note
This setting is only relevant if you use the API listener.
Note
The API listener will always enforce success
and data
to be part of the _serialize
array.
This method is intended to allow you to add additional keys to your API responses with ease. An example of this is the API Query Log.
To get the current configured serialize
keys call the serialize
method without any arguments.
$this->Crud->action()->serialize();
To change the serialize keys, pass a string
or an array
as first argument.
If a string is passed, it will be cast to array
automatically.
$this->Crud->action()->serialize(['my', 'extra', 'keys']);
This is a list of events emitted from the Index Crud Action
.
Please see the events documentation for a full list of generic properties and how to use the event system correctly.
Called after the Controller::beforeFilter()
and before the Crud action.
It’s emitted from CrudComponent::startup()
and thus is fired in the same cycle
as all Component::startup()
events.
Triggered when a CrudAction
is going to handle a CakePHP request.
It’s emitted from CrudComponent::beforeFilter
and thus is fired in the same cycle as all Controller::beforeFilter
events.
This event is emitted before Controller::paginate()
is called.
public function index()
{
$this->Crud->on('beforePaginate', function(\Cake\Event\EventInterface $event) {
$event->getSubject()->query->where(['is_active' => true]);
});
return $this->Crud->execute();
}
This event is emitted right after the call to Controller::paginate()
.
The entities
property of the event object contains all the database records found in the pagination call.
public function index()
{
$this->Crud->on('afterPaginate', function(\Cake\Event\EventInterface $event) {
foreach ($event->getSubject()->entities as $entity) {
// $entity is an entity
}
});
return $this->Crud->execute();
}
Invoked right before the view will be rendered.
This is also before the controllers own beforeRender callback.