Laravel folder structures

走远了吗. 提交于 2019-12-06 01:01:01

问题


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?


回答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.




回答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';



回答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.



来源:https://stackoverflow.com/questions/21379434/laravel-folder-structures

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!