Mauricio Dziedzinski Mauricio Dziedzinski - 1 month ago 14
reST (reStructuredText) Question

Best practices to load multiple views for same resource

I'm having a bad time figuring out the best way to load multiple views of the same resource in Laravel. I'll explain the problem:

I have a resource called Books, which has a controller called BooksController with the basic restfull methods (index, store, etc.). Now I want that, in my home page, these books to be loaded with different stuff from different resources, but I also want these books to be loaded with different resources in different pages too, and this is where I'm having trouble finding the controllers I should use.

When should I use the BooksController GET requests, since it will never only show something related to the books only?

Should I create a controller for each page? Like a HomeController to show the books with the different resources, and the same for any other -Page-Controller, leaving the BooksController only for updating and storing?

Maybe I should not use rest at all and just create a controller for each page?

I really need a light here to help me better understand this Pages and Controllers flow using MVC and REST, thanks in advance!

Answer

Here's how i would structure it:

  • Have a controller handle any CRUD operations for each of your Resources (One controller for your Book, one for your other resource)
  • Have a controller handle your views, this controller would be the one where you mix each of your resources to create the view you want

It is OK to separate your controllers to handle specific logic as long as you keep some sort of convention to keep track of this, like naming your controllers in a specific way so you can quickly differentiate them.

You could structure your Resource Controller to act as an API, and the get requests will only return JSON. Which would also make it easier for your frontend javascript to interact with your resources.

Comments