Configuration

Configuration of Crud is done through the Crud component - either on the fly anywhere in you application, or by providing the configuration in the Controller::loadComponent() method.

Assuming you have followed the installation guide we will now begin the actual configuration of Crud.

class AppController extends \Cake\Controller\Controller
{

  public function initialize()
  {
      parent::initialize();

      $this->loadComponent('Crud.Crud');
  }
}

At this time, the Crud Component is loaded, but we need to tell Crud which actions we want it to handle for us.

Actions

The list of actions is provided either as Component configuration, or on the fly.

An example configuration for handling index actions looks like this.

class AppController extends \Cake\Controller\Controller
{

  use \Crud\Controller\ControllerTrait;

  public function initialize()
  {
      parent::initialize();

      $this->loadComponent('Crud.Crud', [
          'actions' => [
              'Crud.Index'
          ]
      ]);
  }
}

An example of on the fly enabling an Crud action:

class AppController extends \Cake\Controller\Controller
{

  public function beforeFilter(\Cake\Event\Event $event)
  {
      $this->Crud->mapAction('index', 'Crud.Index');
  }
}

The examples above are functionally identical, and instructs Crud to handle the index action in controllers using Crud.Index action class.

Note

If you do not wish for Crud to be enabled across all controllers, or even use all actions provided by Crud you can pick and chose which to use. Crud will not force take-over any application logic, and you can enable/disable them as you see fit.

Action configuration

Note

Each Crud Action can have a different set of configuration settings, please see their individual documentation pages for more information.

A more verbose example now, where we’ll change the view template that Crud will use for index actions to be my_index.ctp

class AppController extends \Cake\Controller\Controller
{

  use \Crud\Controller\ControllerTrait;

  public function initialize()
  {
      parent::initialize();

      $this->loadComponent('Crud.Crud', [
          'actions' => [
              'index' => [
                'className' => 'Crud.Index',
                'view' => 'my_index'
              ]
          ]
      ]);
  }
}

An example of on the fly enabling a Crud action with configuration

class AppController extends \Cake\Controller\Controller
{

  public function beforeFilter(\Cake\Event\Event $event)
  {
      $this->Crud->mapAction('index', [
        'className' => 'Crud.Index',
        'view' => 'my_index'
      ]);
  }
}

Disabling loaded actions

If you’ve loaded an action in eg. your AppController - but don’t want it included in a specific controller, it can be disabled with the $this->Crud->disable(['action_name']).

Example of disabling a loaded action, first we show all actions being configured to be handled by Crud, then disabling a specific action in our PostsController.

class AppController extends \Cake\Controller\Controller
{

  use \Crud\Controller\ControllerTrait;

  public function initialize()
  {
      parent::initialize();

      $this->loadComponent('Crud.Crud', [
          'actions' => [
              'Crud.Index',
              'Crud.View',
              'Crud.Delete',
              'Crud.Edit'
          ]
      ]);
  }
}
class PostsController extends AppController
{

    public function beforeFilter(\Cake\Event\Event $event)
    {
        parent::beforeFilter($event);

        $this->Crud->disable(['Edit', 'Delete']);
    }
}

Built-in actions

Crud provides the default create, read, update and delete actions out of the box.

Custom action classes

It’s possible to create your own custom action classes as well, or overwrite the built-in ones. Simply provide the className configuration key for an action, and Crud will use that one instead.

class AppController extends \Cake\Controller\Controller
{
    use \Crud\Controller\ControllerTrait;

    public function initialize()
    {
        parent::initialize();

        $this->loadComponent('Crud.Crud', [
            'actions' => [
                'index' => ['className' => '\App\Crud\Action\MyIndexAction'],
                'view' => ['className' => '\App\Crud\Action\MyViewAction']
            ]
        ]);
    }
}

Note

Ensure that you escape your namespace when loading your own action classes.

Learn more about custom action classes.

Listeners

The other way to customise the behavior of the Crud plugin is through it’s many listeners. These provide lots of additional functionality to your scaffolding, such as dealing with api’s and loading related data.

Check the Listeners documentation for more on Crud’s included listeners, and how to create your own.

Prefix routing

You might have a scenario where you’d like to use Crud, but only within a certain prefix, such as running your admin area on Crud under the admin prefix.

The easiest way to achieve this is to create an AppController for the prefix, and have your other prefixed controllers extend from that one. Then you can configure Crud in your prefixes AppController.

Let’s look at an example, using an api prefix. For this example, we’ll assume your prefix routing is already configured.

First step is to create your new ApiAppController which should be in src/Controller/Api/.

namespace App\Controller\Api;

class ApiAppController extends Controller
{
    public function initialize()
    {
        $this->loadComponent('Crud.Crud', [
            'actions' => [
                'Crud.Index',
                'Crud.View'
            ]
        ]);

        $this->Crud->addListener('Crud.Api');
        $this->Crud->addListener('Crud.ApiPagination');
    }
}

So now that we’ve created our new ApiAppController we can extend the other prefix controllers from this one, so that they inherit the Crud configuration without impacting other areas of our application.

namespace App\Controller\Api;

class ProductsController extends ApiAppController
{
}
  v: latest
Versions
latest
stable
5.1.3
5.1.2
5.1.1
5.1.0
5.0.0
4.4.4
4.4.3
4.4.2
4.4.1
4.4.0
4.3.5
4.3.4
4.3.3
4.3.2
4.3.1
4.3.0
v4.0.0
cake3
Downloads
pdf
htmlzip
epub
On Read the Docs
Project Home
Builds
Downloads
On GitHub
View
Edit

Free document hosting provided by Read the Docs.