Closed
Description
Operation = some action the user can perform on the Model, that could include an interface (so route, controller method, etc) but also some operation on the entry (so something to call inside the CrudPanel object);
Example: Create operation. Update operation. Delete operation. Reorder operation.
One operation can consist of:
- one/more routes;
- those routes could go to one/multiple EntityCrudController methods (called actions);
- methods and properties on the CrudPanel object;
- form validation files, or other parameters;
What I propose is that we isolate each operation into its own class, so that the definition in EntityCrudController::setup()
would change to something like this:
class ProductCrudController extends CrudController
{
public function setup()
{
/*
|--------------------------------------------------------------------------
| BASIC CRUD INFORMATION
|--------------------------------------------------------------------------
*/
$this->crud->setModel('App\Models\Test');
$this->crud->setRoute(config('backpack.base.route_prefix') . '/test');
$this->crud->setEntityNameStrings('test', 'tests');
/*
|--------------------------------------------------------------------------
| BASIC CRUD INFORMATION
|--------------------------------------------------------------------------
*/
$createFields = [
[ // Text
'name' => 'title',
'label' => "Title",
'type' => 'text',
],
[ // Textarea
'name' => 'description',
'label' => 'Description',
'type' => 'textarea'
],
[ // Address
'name' => 'address',
'label' => 'Address',
'type' => 'address',
// optional
'store_as_json' => true
],
];
$updateFields = $createFields;
$updateFields[] = [
[ // Textarea
'name' => 'observations',
'label' => 'Observations',
'type' => 'textarea'
],
];
$this->crud->enable(new CreateOperation($createFields));
$this->crud->enable(new UpdateOperation($updateFields));
$this->crud->enable(new DeleteOperation);
$this->crud->enable(new PreviewOperation);
$this->crud->enable(new NestOperation('title', 0));
}
}
This enable()
would:
- attach more methods to the current controller (specified inside the operation object);
- attach more methods/properties to the CrudPanel object (specified inside the operation object);
I think this could:
- make it easier to create reusable operations (create a class that extends Operation and enable it in the controller);
- make it clearer which fields are used where;
- make it easier to define stuff that's mandatory for an operation (because it's an object, configuration is passed as parameters and IDE autocomplete will help);
- make it more obvious which operations are enabled for each EntityCrudController;