Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the correct way to break a larger Laravel 5 project into modules?

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?

like image 901
iamrobin Avatar asked Mar 14 '15 11:03

iamrobin


2 Answers

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

like image 75
Marcin Nabiałek Avatar answered Oct 30 '22 03:10

Marcin Nabiałek


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).

  • Otherwise, do what I did and Google Opensource Laravel projects. You get good insight from them. Especially those that a featured or are organised, with growing communities.
like image 38
user919426 Avatar answered Oct 30 '22 02:10

user919426