应用程序结构
简介
默认的 Laravel 应用程序结构是为了给无论构建大型还是小型应用程序都提供一个良好的开始。当然,你可以依照喜好自由地组织应用程序。Laravel 几乎没有限制任何类(class)文件的放置位置 - 只要 Composer 可以自动加载这些类即可。
根目录
一个全新创建的 Laravel 项目的根目录中包含以下子目录:
app
目录,如你所料,包含应用程序的核心代码。我们之后将会很快深入地探讨这个目录的细节。
bootstrap
目录包含的几个文件用于启动框架和配置自动加载功能,还有一个 cache
目录,用于存放框架自动生成的文件,能够加速框架启动。
config
目录,顾名思义,包含所有应用程序的配置文件。
database
目录包含了数据库迁移与数据填充文件。如果你不介意的话,也可以将 SQLite 数据库文件存放在这个目录中。
The public
directory contains the front controller and your assets (images, JavaScript, CSS, etc.).
public
目录包含前面的控制器和你的资源文件 (图片、JavaScript、CSS,等等)。
resources
目录包含你的视图、原始的资源文件 (LESS、SASS、CoffeeScript) 和本地化语言文件。
storage
目录包含编译后的 Blade 模板、基于文件的 session、文件缓存和其他由框架生成的文件。此目录下面包含三个子目录:app
、framework
和 logs
。app
目录用户存放应用程序所用到的任何任何文件;framework
目录用于存放由框架生成的文件和缓存文件;最后,logs
目录用于存放应用程序的日志文件。
tests
目录用于存放你的自动化测试文件。Laravel 默认自带了一个 PHPUnit 的实例。
vendor
目录用于存放 Composer 的依赖包。
App 目录
The "meat" of your application lives in the app
directory. By default, this directory is namespaced under App
and is autoloaded by Composer using the PSR-4 autoloading standard.
You may change this namespace using the app:name
Artisan command.
The app
directory ships with a variety of additional directories such as Console
, Http
, and Providers
. Think of the Console
and Http
directories as providing an
API into the "core" of your application. The HTTP protocol and CLI are both mechanisms to interact with your application, but do not actually contain application logic. In other words, they are simply two ways of issuing commands
to your application. The Console
directory contains all of your Artisan commands, while the Http
directory contains your controllers, filters, and requests.
The Jobs
directory, of course, houses the queueable jobs for your application. Jobs may be queued by your application, as well as be run synchronously within the current request lifecycle.
The Events
directory, as you might expect, houses event classes. Events may be used to alert other parts of your application that a given action has occurred, providing a great deal of flexibility
and decoupling.
The Listeners
directory contains the handler classes for your events. Handlers receive an event and perform logic in response to the event being fired. For example, a UserRegistered
event might be handled by a SendWelcomeEmail
listener.
The Exceptions
directory contains your application's exception handler and is also a good place to stick any exceptions thrown by your application.
注意: Many of the classes in the
app
directory can be generated by Artisan via commands. To review the available commands, run thephp artisan list make
command in your terminal.
为应用程序设置命名空间
As discussed above, the default application namespace is App
; however, you may change this namespace to match the name of your application, which is easily done via the app:name
Artisan command. For example, if your
application is named "SocialNet", you would run the following command: 如前面所提到的,默认的应用程序命名空间为 App
;然而,你还可以以应用程序的名称来作为命名空间,这可以简单地通过 app:name
Artisan 命令来完成。例如:如果你的应用程序叫做 "SocialNet",你需要执行下面的命令:
php artisan app:name SocialNet
当然,你仍然可以继续快乐地使用 App
命名空间,