phplaravelcontrollersapplication-design

Controllers by page or controllers by entity?


I'm building in Laravel a self managed institutional website with some dynamic content like: products, news, users, etc., and I came up with a doubt about controllers and best practices.

While some pages are fully static (about, services info, ...) others, like products listing, are dynamic.

I'm not sure if I have to create controllers by page or entities. Eg:

ProductController => shows the page for a single product
ProductsController => shows the page for listing all the products
SearchController => shows the page for searching products
AboutController => shows the about page
ServicesController => shows the page for listing services

vs

ProductsController => shows the page for single and multiple products, and the search products page
PagesController / StaticController => shows all static pages (like "about" and "services")

What is the best approach for this?


Solution

  • Controller will be class responsible for displaying entity values. So You should use your second example, controllers by entity.

    You have for example functionality "Show my static site", this is one functionality, one responsibility, so one class/controller. If you divide it for many controllers, that gave You duplicate code.