You are busy, and you just want to get things done™
, so let’s get going.
After installation, you are ready to CRUD-ify your app.
If you haven’t configured the CRUD plugin already, add the following lines to your
src/Controller/AppController.php
file
<?php
declare(strict_types=1);
namespace App\Controller;
class AppController extends \Cake\Controller\Controller
{
use \Crud\Controller\ControllerTrait;
public function initialize(): void
{
parent::initialize();
$this->loadComponent('RequestHandler');
$this->loadComponent('Flash');
$this->loadComponent('Crud.Crud', [
'actions' => [
'Crud.Index',
'Crud.View',
'Crud.Add',
'Crud.Edit',
'Crud.Delete',
'Crud.Lookup',
],
'listeners' => [
// New listeners that need to be added:
'CrudView.View',
'Crud.Redirect',
'Crud.RelatedModels',
// If you need searching. Generally it's better to load these
// only in the controller for which you need searching.
'Crud.Search',
'CrudView.ViewSearch',
]
]);
}
/**
* Before render callback.
*
* @param \Cake\Event\Event $event The beforeRender event.
* @return void
*/
public function beforeRender(\Cake\Event\EventInterface $event)
{
if ($this->viewBuilder()->getClassName() === null) {
$this->viewBuilder()->setClassName('CrudView\View\CrudView');
}
}
}
If you are familair with the CRUD plugin already, you will immediately understand that Crud view is simply a listener for the events generated by the plugin. If this is new to you, don’t worry, it will be explained in the following sections.
Any controller inheriting from AppController
will automatically implement
the specified actions loaded int the CRUD
component configuration.
Therefore, you can just leave your controller code empty!
<?php
namespace App\Controller;
class CategoriesController extends AppController
{
// No code here, but we have all actions available to use!
}
You can now access your categories list by pointing your browser to
http://example.com/categories
. Browse around your new Admin interface for
each of the controllers you have in your application.