Laravel folder structures

11,918

Solution 1

Best way to master Laravel folder structure is to treat app directory as a front end of framework. If you take a look at the git repository you'll see that they are separated - you can clone core library and you can clone laravel application alone. Application, with it's subfolders represents just one way in which framework can be used. Ofcourse, it is designed with best practices involved. Check out also core framework tests directory - there Laravel developers treated library as "headless" - without application. For me, it was everything I need to grasp Laravel.

So you are free to modify existing structure, but keep in mind that some changes require you to composer dump-autoload - mostly because of namespaces.

Solution 2

By and large, Laravel can be structured in whatever way works best for you. Some people prefer the default architecture while other like domain-based architecture.

Both of my current projects deviate from this, using something like:

/app
  /database
  /controllers
  /bin
  /views
  /config
  /storage

I keep a lot of my custom functionality in a service provider, although I have some generic helpers in the bin/ along with my routes and filters.

You can do whatever you want, just make sure you accordingly update your composer.json and app/start/global.php to make sure the proper classes are autoloaded. And make sure you namespace everything correctly!

Below is an example of the relevant sections of my composer.json and global.php just for an idea:

composer.json:

"autoload": {
    "classmap": [
        "app/commands",
        "app/controllers",
        "app/database/models",
        "app/database/migrations",
        "app/database/seeds"
    ]
},

app/start/global.php:

ClassLoader::addDirectories(array(

app_path().'/commands',
app_path().'/controllers',
app_path().'/bin',
    app_path().'/database/models',
    app_path().'/database/seeds',

));
require app_path().'/bin/filters.php';
require app_path().'/bin/helpers.php';
require app_path().'/bin/events.php';

Solution 3

in short, it's completely a matter of preference and how you and your team see it best for readability, That being said it's good to have consistency across all your projects.

Share:
11,918
AndrewMcLagan
Author by

AndrewMcLagan

I’m a full stack Web Developer with over fifteen years experience in the industry. I concentrate on open source languages and technologies such as PHP 5.6+, Python, Javascript EMCA6, AngularJS, React, Laravel and Symfony. My strengths would be a very strong interest in the open source community and approaching web­app development from a software design perspective rather than simply being a code­monkey. Strong understanding of basic SOLID principals and the patterns that make these principles a reality. Coming from many years experience as a contract developer, keeping up­-to-­date with the open source community, tools and best practices has been paramount to my career. Also, I LOVE to code!

Updated on June 04, 2022

Comments

  • AndrewMcLagan
    AndrewMcLagan almost 2 years

    Often i come find it problematic when deciding where to place folders to resources within the app\ folder.

    Where should i place things such as model observers and validators and form macros and repositories.... currently i do the following

    \app
       \models
       \controllers
       \repositories
       \observers
       \interfaces
       \validators 
       \views
    

    although i see some people do the following:

    \app
       \models
       \controllers
       \views
       \YourAppNameHere
          \Services
             \validators
             \...
    

    I do not understand the reason behind the \Acme folder when its the same as the actual application?

  • AndrewMcLagan
    AndrewMcLagan over 10 years
    the main problem with being so open to any structure is.... that does not really help
  • AndrewMcLagan
    AndrewMcLagan over 10 years
    Thank you, that is a really good place to start looking. I guess my core concern is that i often see people on the web structuring their folders under /lib/validators ... /lib/macros ... and as i see it everything that is the "application domain" should reside under /app and appropriate name spaces created. Im just looking for the best, clearest and most "eloquent" structure.