I'm working on a PHP project with Laravel 5 and I'm thinking of setting up a different folder structure for it.
The standard Laravel folder structure is something like this:
/app
/commands
/Http
/Controllers
/Middleware
Kernel.php
routes.php
/Providers
Model.php
/config
/resources
etc...
However, when the project grows larger and you have a lot of Controllers/Repositories/Models and such. This structure is gonna break.
For example: it's not very easy to find a bug in your admin panel if you have to dig through your routes, find which controller is responsible, find that controller amongst a large set of controllers, find out what that does, find out other possibly responsible classes in other large folders, and so on. In short: it's a mess.
I've been looking at ways to break the structure into modules. I've come up with a way to do it, but I'm not sure if it's a good way.
I would make a folder each functionality and put all the related code together. For example:
/app
/Admin
/Controllers
/Requests
/Models
routes.php
/Products
/Controllers
/Requests
/Models
routes.php
etc. (you get the point)
Instead of initializing 1 router from the standard RouteServiceProvider.php, I would have to write a ServiceProvider for each module and start all individual routes from there. So in this case I would have an AdminServiceProvider and a ProductServiceProvider which each require the routes.php file in their own subdirectory (and with that their own controller namespace).
This seems to solve my case for now, but I'm wondering if I'm gonna run into trouble with this setup. All the examples I can find on the web just stick to the standard structure. Can anyone tell me if this is a decent way to do it? Or does anyone have an alternate way of doing this?
This is a good way you proposed but there is no need to do it on your own. At the moment I'm working on a L5 project that uses modules - each of them have directories for repositories, models, own route file etc. I'm using Caffeinated module for that
I also believe the structure looks good. I would also add on an additional, core or base folder which will contain base classes and reusable componets, to ensure you do not have duplications of code in the separate "modules"
You can also checkout this interesting presentation at a Laracon, September 2014: Laracon 2014: Dayle Rees - Breaking The Mold
Also have a look at the source for October CMS and Doptor CMS(claims to be moduler), you may glean a thing or two. (Have used neither, by the way).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With