CRUD Controllers

Crud Name

Once you have created a new Crud Controller, you may need to set the $crud_name. This is what allows your controller to find the correct config() data. You will need to do this if the $crud_name cannot be inferred from a lower-snake-cased version of the Controller class name, less the word Controller).

For example, if your new Controller is called RegionController and you want the $crud_name to be region, you do not need to do anything.

If you require it to be something else, set the protected $crud_name property

protected $crud_name = 'override_crud';

As of Ensō 3.3 This is no longer a feature and crud_name is determined solely from the crud configuration files.

Permissions

You can set Permissions for each Crud action create, read, update and destroy. This we ensure that your controller returns a 403 responnse when someone without permissions attempts to access a relevant route.

These should be set directly in the $permissions array as:

  • a string. This will be the slug of a single permission that is required to access the action.
  • an array. This should be an array of slugs of permissions which should enable access to this action.

Or alternatively as a callable via the constructor. Such a callable should return true or false dependent on whether the action is permitted. It should expect the relevant $config instance as the first argument, and the \$item that is being acted upon (where appropriate) as the second.

Examples:

/**
 * Array for determining Crud action permissions
 *
 * @var array
 */
protected $permissions = [
    'create' => 'full-cms-access',
    'read' => ['full-cms-access', 'partial-cms-access'],
    'update' => null,
    'destroy' => null,
];

/**
 * Create a new instance of CrudController
 *
 * @return void
 */
public function __construct()
{
    parent::__construct();

    $this->setPermission(
        'update',
        function ($config, $item) {
            return Auth::check()
                && Auth::user()->canModifyPage($item);
        }
    );

    // OR

    $this->setPermissions(
        [
            'update' => function ($config, $item) {
                return Auth::check()
                    && Auth::user()->hasPermissions(['full-cms-access', 'write-cms-access']);
            },
            'delete' => function ($config, $item) {
                return $item->userCanDelete();
            }
        ]
    );
}

Index route data format

As of Ensō 3.3 xhr data from the admin crud routes has been stardardised to be passed through Resources.

If you pass a table parameter in your query string, the controller will return data passed into a \Yadda\Enso\Crud\Resources\TableResource::collection($items), which by default calls toArray on each Model.

You may change the data format (but not the full response) per controller by overriding the makeTableCollection($items) function.

IF you do not pass a table parameter, the controller will assume you are searching for list data (to populated a SelectField, for example). By default, the controller will pass data which has been run through \Yadda\Enso\Crud\Resources\ListResource::collection($items).

You may change the data format (which comprises the full results that the controller returns, unlike the table variant) per controller by overriding the makeListCollection($items) function.