ResourceController Lifecycle
The ResourceController is the base class for quickly creating CRUD controllers. To allow flexibility without losing automation, it features a Hooks system.
Execution Flow (Lifecycle)
When doIndex() (or any default action) is executed:
- detectMode(): Determines if it is List or Edit mode (based on GET/POST parameters).
- beforeConfig(): [HOOK] Executed before building the configuration. Useful for defining global variables.
- buildConfiguration(): Builds the structure of columns, fields, and tabs.
- checkTableIntegrity(): (Optional) Verifies that the table exists.
- setup(): Adds default buttons (New, Save).
- Mode-Specific Hooks:
- If LIST MODE -> beforeList()
- If EDIT MODE -> beforeEdit()
- handleRequest(): Processes the actual request (AJAX get_data, save, etc.).
- renderView(): Displays the Blade template.
How to Use Hooks
beforeEdit()
Use it to inject additional data into the edit view or change the default template.
php
protected function beforeEdit()
{
// Load extra data
$extraData = MyModel::all();
$this->addVariable('extraData', $extraData);
// Change the template if you need a fully customized form
$this->setDefaultTemplate('page/my_custom_edit');
}beforeList()
Use it to modify columns or filters dynamically based on the user or state.
php
protected function beforeList()
{
if ($this->user->is_superadmin) {
$this->addListColumn('general', 'deleted_at', 'Deleted at');
}
}