question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Issue with Controller inheritance / nested resources

See original GitHub issue

So 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:closed
  • Created 7 years ago
  • Comments:23 (10 by maintainers)

github_iconTop GitHub Comments

2reactions
DennisCraandijkcommented, Oct 4, 2016

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:

    1. don’t use find or fail
    2. or use
    if ( ! \App::runningInConsole()) {
    
    }
    
  • Thefirst parameter of the route is being used as $id. Possible solutions:

    1. overwrite the affected methods as follows
    public function edit($id) {
        return parent::edit(\Route::current()->parameter('post'));
    }
    
    1. or make the desired parameter the first parameter by adjusting the route (dirty solution, but quick)
    Route::current()->forgetParameter('user');
    
0reactions
tabacitucommented, Apr 3, 2017

Hi @mrvanwagoner ,

This tutorial might help: Nested Resources in Backpack CRUD.

Cheers!

Read more comments on GitHub >

github_iconTop Results From Across the Web

How can I pass parameters to nested resources for single ...
I am having some difficulties passing in parameters to my controller. I created an Single table inheritance model in my model file.
Read more >
Cascade, specificity, and inheritance - Learn web development
We'll start with inheritance. In the example below, we have a <ul> element with two levels of unordered lists nested inside it.
Read more >
Nested Routes | Vue Router
With Vue Router, you can express this relationship using nested route configurations. ... Note that nested paths that start with / will be...
Read more >
Action Controller Overview - Ruby on Rails Guides
If you limit your controllers to only RESTful Resource Routing actions you should not need to worry ... Note the nested hash in...
Read more >
Views in ASP.NET Core MVC | Microsoft Learn
To learn more, see the Layout topic. ... Since most controllers inherit from Controller, you simply use the View helper method to return...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found