【筆記-Laravel】Laravel的目錄結構
底下表格是從Laravel官方文件複製過來的5、6、7版的目錄結構,完全一樣,沒有變動。
5.8 | 6.x | 7.x |
The Root Directory
The App Directory
放置應用程式的核心程式碼,此部份目錄底下目錄細節在後敘述。The Bootstrap Directory
此目錄包含用來啟動框架的app.php
檔案,此目錄亦放置了cache
目錄,包含了框架為了效能最佳化所產生的檔案。
The Config Directory
應用程式設置目錄。The Database Directory
包含資料庫遷移、模型工廠、以及種子,如果有必要的話,可以在這個目錄放置SQLite資料庫。The Public Directory
放置index.php
檔, 應用程式的進入點,其中定義了自動載入機制(autoloading),這個目錄也放置了如images, JavaScript, and CSS等檔案。
The Resources Directory
放置了所有的 views 檔案,以及那些未經編繹的LESS, SASS, 或 JavaScript,這個目錄也放置了語系檔案。The Routes Directory
包含了所有應用程發的路由定義,Laravel預設底下幾個檔案:web.php
, api.php
, console.php
and channels.php
.
web.php
檔案包含了包含了由 RouteServiceProvider
所產生放置於web中介軟體群組的路由檔案,這些路由提供了會談狀態、CSRF 保護機制、以及cookie加密。 如果應用程式未包含一個無狀態的 RESTful API,所有的路由將會大致放置於 web.php
檔案。
api.php
檔案包含那些由RouteServiceProvider
放置於 api
中介軟體群組的路由檔案,這些路由提供了rate limiting。這些路由基本上是無狀態的,使得透過這些路由進到應用程式的所有請求會透過tokens被authenticated(身份認證),,並且無法存取會談 session 狀態。
console.php
用來定義所有閉包中控台指令,每個閉包指令繫結一個命令實例,充許一個簡單的方法與每一個指令IO方法互動,即使這個檔案沒有定義 HTTP 路由,它定義了中控台模式的應用程式進入點(路由)。
channels.php
註冊應用程式所支援的事件廣播頻道。
The Storage Directory
此目錄包含了所有編譯過的Blade樣板、檔案型式的狀態、檔案快取、及其他由框架(Laravel)所產生的檔案。 這個目錄分成app
, framework
, p及 logs 幾個目錄。app目錄可以用來儲存應用程式所產生的任何檔案,framework目錄用來儲存由框架所產生的檔案及快取資料,最後,logs目錄包含了應用程式的記錄檔。
storage/app/public
目錄可以用來儲存使用者產生的檔案,像是avatars個人資料檔案,這些本來就應該開放大眾存取的。你應該在public/storage
建立一個symbolic link指向這個目錄,可以用" php artisan storage:link
"命令建立這個目錄符號連結 (虛擬目錄的意思)。
The Tests Directory
此目錄包含了自動測試案例,PHPUnit 是一個立即可用的測試範例,每個測試類別都應用Test
結尾,你可以使用 phpunit
或 php vendor/bin/phpunit
指令來執行你的測試。
The Vendor Directory
此目錄放置 Composer 依存資料。The App Directory
這個目錄是應用程式的主要部份,框架使用PSR-4 autoloading standard 來自動載入這個目錄。app
目錄包含了各種不同的子目錄,像是 Console
, Http
, 和Providers
,Console
和 Http
目錄內含提供進入應用程式核心的API。 HTTP 協定和CLI 都是用來和應用程式互動的機制,但是並不實際包含應用程式邏輯。換句話說,有二種方式發出命令給應用程式, Console
目錄包含所有的Artisan(工匠)命令, 而Http
目錄則是用來包含應用程式的控制器/controllers, 中介軟體/middleware, 和請求/requests。
當你使用make工匠(artisan)命令建立類別時,會在app目錄下產生各種不同的子目錄,例如 ,app/Jobs
目錄會在你使用 make:job
工匠/Artisan 命令建立一個job/工作類別後被建立。
Many of the classes in the app
目錄下的許多類別可以透過Artisan命令建立,可以執行php artisan list make
命令來知道可用的命令。
The Broadcasting Directory 廣播目錄
TheBroadcasting
directory contains all of the broadcast channel classes for your application. These classes are generated using the make:channel
command. This directory does not exist by default, but will be created for you when you create your first channel. To learn more about channels, check out the documentation on event broadcasting. 預設這個目錄是不存在的,除非你執行了make:channel命令。在一個頻道裏,事件可以被廣播…
The Console Directory 中控台目錄
TheConsole
directory contains all of the custom Artisan commands for your application. These commands may be generated using the make:command
command. This directory also houses your console kernel, which is where your custom Artisan commands are registered and your scheduled tasks are defined.
The Events Directory 事件目錄
This directory does not exist by default, but will be created for you by theevent:generate
and make:event
Artisan commands. The Events
directory 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 Exceptions Directory 例外目錄
這個目錄是用來放置應用程式的例外處理,也是一個好的地方用來放置任何你應用程式所丟出的任何例外。如果你希望客制化例外如何被記錄或生成,你應該修改這個目錄下的Handler類別。 TheExceptions
directory contains your application's exception handler and is also a good place to place any exceptions thrown by your application. If you would like to customize how your exceptions are logged or rendered, you should modify the Handler
class in this directory.
The Http Directory
幾乎所有用來處理應用程式請求的邏輯都被放在這個目錄中。 TheHttp
directory contains your controllers, middleware, and form requests. Almost all of the logic to handle requests entering your application will be placed in this directory.
The Jobs Directory
這個目錄用來放置應用程式的可排隊工作,應用程式的工作可以用排隊的方式執行,或者在現有的請求生命週期中同步執行。 This directory does not exist by default, but will be created for you if you execute themake:job
Artisan command. The Jobs
directory houses the queueable jobs for your application. Jobs may be queued by your application or run synchronously within the current request lifecycle. Jobs that run synchronously during the current request are sometimes referred to as "commands" since they are an implementation of the command pattern. 那些同步執行的工作有時被看做是命令,因為這些工作是command pattern的一個實作。
The Listeners Directory 事件監聽器目錄
這個目錄包含了所有事件的處理,事件監聽器接收到一個事件後,會執行對應於事件的處理邏輯。 This directory does not exist by default, but will be created for you if you execute theevent:generate
or make:listener
Artisan commands. The Listeners
directory contains the classes that handle your events. Event listeners receive an event instance and perform logic in response to the event being fired. For example, a UserRegistered
event might be handled by a SendWelcomeEmail
listener.
The Mail Directory 郵件目錄
這個目錄包含了所有應用程式所發出的郵件。 This directory does not exist by default, but will be created for you if you execute themake:mail
Artisan command. The Mail
directory contains all of your classes that represent emails sent by your application. Mail objects allow you to encapsulate all of the logic of building an email in a single, simple class that may be sent using the Mail::send
method.
The Notifications Directory 通知目錄
這個目錄包含所有由應用程式發出的交易通知,例如,一個在應用程式內部發生的事件通知。Lavavel的通知機制抽象化了在email, Slack, SMS,或儲存於一個資料庫中的通知發送。 This directory does not exist by default, but will be created for you if you execute themake:notification
Artisan command. The Notifications
directory contains all of the "transactional" notifications that are sent by your application, such as simple notifications about events that happen within your application. Laravel's notification features abstracts sending notifications over a variety of drivers such as email, Slack, SMS, or stored in a database.
The Policies Directory 政策目錄
This directory does not exist by default, but will be created for you if you execute themake:policy
Artisan command. The Policies
directory contains the authorization policy classes (授權政策類別) for your application. Policies are used to determine if a user can perform a given action against a resource. For more information, check out the authorization documentation. 這些規則用來決定一個使用者是否有權限在一個資源上執行動作。
The Providers Directory 提供者目錄
此目錄包含了所有服務提供者… TheProviders
directory contains all of the service providers for your application. Service providers bootstrap your application by binding services in the service container, registering events, or performing any other tasks to prepare your application for incoming requests.
In a fresh Laravel application, this directory will already contain several providers. You are free to add your own providers to this directory as needed. 在一個剛建立的Laravel應用程式,這個目錄會包含數個提供者。我們可以依自己的需要來加入所需要的提供者。
The Rules Directory 規則目錄
此目錄包含了應用程式客制化的確認規則 (輸入資料的正確性確認)。
This directory does not exist by default, but will be created for you if you execute themake:rule
Artisan command. The Rules
directory contains the custom validation rule objects for your application. Rules are used to encapsulate complicated validation logic in a simple object. For more information, check out the validation documentation.