Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel modular structure

I am developing an application using Laravel. However I have question about the modular structure of the application.

Here's an example.

Suppose I need a module called "featured items";

This is a small module that should be included in some controllers.

The thing is I'm not sure how to this? Should this be a new controller which will be called inside other controllers?

Should this be a model? Should this be written as a bundle?

Does anyone have any idea about what is the best approach?

like image 992
Sinan Avatar asked Feb 26 '26 03:02

Sinan


1 Answers

I dont think there's a right answer to this really as it is matter of preference though there are things to bear in mind to let you make an intelligent decision as to where your code should be placed.

Does the module have a small footprint and will function with no more than itself?

  • Create a library

Does the module require it's own routes and views but no requirement for libraries?

  • Create an application controller
  • Use Laravel Routes
  • Use bundle controllers

Does the module contain both the above?

  • Create a bundle with controllers and libraries

It's not as simplistic, other factors should be taken into consideration as well. If the module requires multiple controllers for example, you'd be better off writing it in a bundle. As bundles do not require you to have anything more than start.php file within the bundle directory, you register what you want with the Autoloader. From this you can see a lot of peoples preferences would be to build all their modular code in bundles.

like image 65
David Barker Avatar answered Feb 28 '26 16:02

David Barker