strmizo's avatar

Where to put models in Laravel 5 ?

Hello guys ! I know models are in /app, but i'm trying to build mid to large application, so i want your suggestion about app structure, and namespacing too! I have one app structure in my mind and it's like :

/app
    ...
    /Commands
        /User
            UserRegistrationCommand.php
    /Handlers
        /Commands
            /User
                UserRegistrationCommandHandler.php
    /Models
        User.php
        Recipe.php
    /Repositories
        UserRepositoryInterface.php
        UserRepository.php
        RecipeRepositoryInterface.php
        RecipeRepository.php

I'm really stuck at the app structure and it's namespacing convention !!

0 likes
15 replies
thepsion5's avatar
Level 25

If you're building a large application, group your classes around your domain concepts first and their role second. For example, In your example, I would have this structure:

/app
    /RecipeBook
        /Domain
            /Recipes
                /Commands
                    CreateRecipeCommand.php
                    RateRecipeCommand.php
                /Handlers
                    CreateRecipeCommandHandler.php
                    RateRecipeCommandHandler.php
                RecipeRepositoryInterface.php
                Recipe.php
            /Users
                /Commands
                    RegisterUserCommand.php
                /Handlers
                    RegisterUserCommandHandler.php
                User.php
                UserRepositoryInterface.php
        /Infrastructure
            /Recipes
                EloquentRecipeRepository.php
            /Users
                EloquentUserRepository.php

In my experience, it's much more useful to organize your code by how likely they are to interact instead of their similarity in function. It's not useful to have all your repositories, models, etc in one namespace, because stepping through your application's execution has you mentally jumping back and forth all over the place.

19 likes
zoransa's avatar

Why not make one model with artisan and see where artisan puts it.

strmizo's avatar

I liked thepsion5 answer ! Thanks guys for responding :D Great community !

russw's avatar

@adnan it shouldn't need to be changed. Namespace should still be app name and all is in app directory so all good.

2 likes
MThomas's avatar

I do think that @thepsion5 accidentally used a L4 folder structure, but you can simply change that (so no messing with PSR needed).

Instead of placing everyting in: /app/RecipeBook, place it in just in /app and set the app namespace name to RecipeBook in L5.

russw's avatar

Oh I see the problem. like @MThomas said change the app namespace to RecipeBook. You do this by running php artisan app:name RecipeBook

thepsion5's avatar

Yep, I haven't done any professional work with L5 yet so I'm still in the habit of putting my code in app/ApplicationName :)

nolros's avatar

@thepsion5 agree depends on size of application, team, etc, I find in large applications trying navigate through large sets of files in becomes confusing and abstracted conceptual directory structures requires thinking. I tend to have my own feature centric directory, below, that contains all my core code and then customize for projects. That said, I'm open to why this is not a good approach, but it works for me. Example:

app
     /myApp (or project)
        /posts (post example, but same for all product features, Users, etc. I remove directories that are not relevant)
            /repository
            /contracts
            /commands
            /services
            /exceptions
            /events
            /documentation (I like to keep project documentation in the relevant areas vs centralized )
            /middleware
            Abstract class
            Post.php (model)
            PostManager.php (management code)

I do something similar for views, assets, etc. I create a myApp in controller, views, etc. and then replicate at least the level 1 myApp directories in each so that I can port them across from app to app or version to version i.e. in Assets I have LESS/myApp/Post/ ...post.less, in views I have views/myApp/Post/main.blade.php, etc.

1 like
christopher's avatar

is this kind of structure also possible with L4 ? Because i have a project that should handle many domains.

So i would have a folder with all it`s content ( controllers / models ) for each domain.

bgarrison25's avatar

Im not 100% sure about the conversion of UserRepositoryInterface.php / UserRepository.php into one repository under the interface directory called EloquentUserRepository.php. Can you explain why this changed and how it would work. You moved the repositories and implied that its an eloquent repository of some sort. I thought the majority of the time you use repositories to get AWAY from eloquent. Also wouldn't you want their interfaces to be in the same folder?

Please or to participate in this conversation.