martinszeltins's avatar

Implement DDD and modules with Laravel

Is it at all possible to implement DDD and modules with Laravel in a way where I could open up the project directory and expect to find top-level directories like users, auth, settings, orders etc. instead of technical directories like models and controllers?

my-project/
- users
- settings
- auth
- orders
- organizations
- restaurants
composer.json
.env

Is this even possible with Laravel? I don't want to have a modules directory where I stuff all my DDD modules, I want them to be top-level.

0 likes
15 replies
martinszeltins's avatar

@Tray2 In this series he put the modules inside modules directory but I want modules to be at the top level

Tray2's avatar

@martinszeltins Why would you want to do that? You are supposed to store all your code inside the app namespace.

1 like
martinbean's avatar

@martinszeltins Then put them in the top level? But that’s just going to get messy in my opinion. I’d definitely be putting them inside a directory like /src or something.

1 like
martinszeltins's avatar

@tray2 @martinbean I want a clean structure. Sure they can go under app/ directory, that would be fine. What I'm trying to do is something similar to how Nuxt does it with layers. Basically, I want my top directory super clean. Maybe composer.json file and .env file, but I want all my resources and config and everything else to be split into modules. So it's fine if I have an app directory, but directly inside of app directory, I would like to create modules, and perhaps one of the modules could be common module, but I don't want a module directory. Is it possible to move all of the junk inside of the app / common module?

martinszeltins's avatar

Basically, I would like to ideally get this directory structure for my Laravel app but how do I do it? Is this possible with Laravel? Perhaps server.php could also be put under the common module

my-project/
  └─ app/
    └─ auth/ (controllers, models, views)
    └─ users/ (controllers, models, views)
    └─ settings/ (controllers, models, views)
    └─ sales-orders/ (controllers, models, views)
    └─ cart/ (controllers, models, views)
    └─ checkout/ (controllers, models, views)
    └─ common/ (controllers, models, views)
      └─ bootstrap/
      └─ public/
      └─ storage/
      └─ tests/
composer.json
.env
.gitignore
server.php
1 like
martinbean's avatar

@martinszeltins Again, you’re better off having something like a /src directory with your modules in instead. Then each module would have a service provider that registers the configuration, routes, etc for that particular module. You’ll also need to update the autoload section in your composer.json file to tell it where to find your modules’ classes.

You can find examples and more information on how to do this in the Laravel docs for package development.

1 like
martinszeltins's avatar

@martinbean /src directory feels dirty to me. I want to be able to open up my project and immediately see what this project is about. I want my modules to be front and center. Can't I put service provider in each module or perhaps in my common/ module?

Tray2's avatar

@martinszeltins Sounds a bit like you would be better off not using a framework like Laravel, I mean you take a standard MVC-ish structure and you rewrite it completely. I fear you are going to get into so many strange issues while doing this, and be forced to make some more or less bad decisions, and not to mention it will be all but impossible to upgrade the framework.

2 likes
martinszeltins's avatar

@Tray2 No, that's not it. I DO want to use Laravel. I think I am looking for something like Symfony Bundles or Nuxt Layers. For example, take a look at Symfony Bundles - https://symfony.com/doc/current/bundles/best_practices.html#directory-structure

Clearly, with Symfony you can put assets and config and src and public - all these directories directly inside your bundle/module.

<your-bundle>/
├── assets/
├── config/
├── docs/
│   └─ index.md
├── public/
├── src/
│   ├── Controller/
│   ├── DependencyInjection/
│   └── AcmeBlogBundle.php
├── templates/
├── tests/
├── translations/
├── LICENSE
└── README.md
martinbean's avatar

@martinszeltins I don’t really know what you’re asking for since you’ve clearly decided what directory structure you want and just knocking back any other suggestions.

If you want to stick all your modules in the root directory, then do so.

2 likes
Tray2's avatar

@martinszeltins Just because you can do something, doesn't mean you should, and I strongly advice you not to deviate from the Laravel conventions. However, you do as you please, but consider what a custom directory structure will coset you.

  1. Any and all updates of Laravel will take a lot longer.
  2. The likleyhood of something breaking during one such upgrade increases.
  3. It will be much harder to get help online when you run in to problems.

There are probably a great many more reasons for not going against convention but those a three big ones.

2 likes
jlrdw's avatar

@martinszeltins Good MVC will do all you need. I agree to keep it in the app folder. You can create more folders under app and do whatever you need.

If symfony is better suited, then perhaps use that framework, but just a suggestion.

Please or to participate in this conversation.