Issue with Controller inheritance / nested resources
See original GitHub issueSo now a new controller extends the CrudController and inherits the index(), edit(), store() etc. methods. If I want to modify the behaviour I extend the methods of the parent CrudController in my new controller (so called Callbacks). I love the idea of this system, because it makes CRUD easily customizable. However, the current implementation disabled the use of implicit route binding. Because the parent methods don’t accept any parameters, so the child methods can’t accept any either.
For example. In my database I have users and posts. So I will have a UserCrudController and a PostCrudController both extending CrudController. Now I would like to able to have a crud interface for all the posts of a certain user. Normally I would route like this
/users/{user}/posts/index
and have a method in de PostCrudController
index (User $user) {
// index users
}
however, because the index() method in the PostCrudController extends the CrudController index() method, it isn’t allowed to accept the $user parameter.
So I think the extensibility and customizability of the package will become better, if you can accept your own parameters in each method. One way of solving this would be renaming the parent methods. So every chilcontroller should implement all the crud methods, and call their parent method. It would look something like this
index(User $user) {
$this->crud->addClause('where', 'user_id', '=', $user->id);
parent::indexCrud()
}
Issue Analytics
- State:
- Created 7 years ago
- Comments:23 (10 by maintainers)
Nope. So to sum it up. Nested CrudControllers in general work really well. But there are 2 issues
findOrFail causes CLI to fail. (Although I don’t know if this is still the case after the L5.3 update when defining the CRUD in the setup() function). Possible solutions:
Thefirst parameter of the route is being used as $id. Possible solutions:
Hi @mrvanwagoner ,
This tutorial might help: Nested Resources in Backpack CRUD.
Cheers!