Skip to content

[Feature Request] Define the model, route and entity name strings inside the CrudResource, not the CrudController #4

Open
@tabacitu

Description

@tabacitu

Feature Request

What I am trying to achieve

What if we could define EVERYTHING we need to define for a CRUD, inside the CrudResource? If you prefer to define a CrudResource, I don't really see a reason why you would split your definition across multiple files:

So what if... if you're using CrudRequest... the CrudController is kind of optional? Only there in case you need to do custom stuff, add things to the setup of a particular operation or override how an operation does its thing?

How I see it implemented

To achieve that, we need to move this from the CrudController:

// BookCrudController.php

    public function setup()
    {
-        CRUD::setModel(\App\Models\Book::class);
-        CRUD::setRoute(config('backpack.base.route_prefix') . '/book');
-        CRUD::setEntityNameStrings('book', 'books');
-
        $this->crudResource = new BookCrudResource();
    }

to the CrudResource:

// BookCrudResource.php


class BookCrudResource extends CrudResource
{
    protected $model = \App\Models\Book::class;
    protected $route = config('backpack.base.route_prefix') . '/book';
    protected $entityNameSingular = 'book';
    protected $entityNamePlural = 'books';

    public function fields(): array
    {
        return [
            Text::make('Name')->size(9),
            Number::make('Year')->size(3),
            Textarea::make('Description')->onlyOnForms(),
            Text::make('ISBN'),
        ];
    }
}

But since some of the above will probably not work (see - calling the config 😅), we'll also need setters/getters for the above, so maybe:

// BookCrudResource.php

class BookCrudResource extends CrudResource
{
    protected $model = 'App\Models\Book'; // mandatory
    protected $entityNameSingular = 'book'; // optional, can figure it out from the model name too
    protected $entityNamePlural = 'books'; // optional, can figure it out from the singular too

    // optional
    public function model() 
    {
        return \App\Models\Book::class;
    }

    // optional, would default to using the entity name as URL segment
    public function route() 
    {
        return config('backpack.base.route_prefix') . '/book';
    }

    // optional
    public function entityNameSingular() 
    {
        return 'book';
    }

    // optional
    public function entityNamePlural() 
    {
        return 'books';
    }

    public function fields(): array
    {
        return [
            Text::make('Name')->size(9),
            Number::make('Year')->size(3),
            Textarea::make('Description')->onlyOnForms(),
            Text::make('ISBN'),
        ];
    }
}

What I've already tried to fix it

Nothing, just spitballing.

Would I be able to work on this myself and submit a PR

Sure, before we launch v6.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions