laravel入門
簡介
作為PHP最常用的框架之一,Laravel的框架目錄布置得尤其清晰,適用于各種類型的項目開發。今天來記錄下laravel入門需要熟悉的知識點。
1、根目錄

其中,public/index.php是項目的入口文件
2、配置

1)config目錄
該文件夾下面,包含的是各種配置文件。包括mysql數據庫連接信息,redis,自定義的配置文件信息等等
2).env文件
用以存儲一些依賴環境的變量,比如數據庫配置,因為它不會被加入到版本庫中, 所以還用以配置一些敏感信息:比如正式環境的一些第三方應用賬號,token 等。有點類似Yii框架中的main-local.php
用法參考:env('DB_HOST','192.168.1.223')
說明:優先使用.env文件中配置的DB_HOST對應的值,如果.env中沒有配置,則使用這里設置的默認值'192.168.1.223'

3)用法參考
config('redis_keys.redis_keys.all_follow_user')
3、MVC

4、路由
1、routes目錄
routes目錄包含了應用定義的所有路由。Laravel 默認提供了四個路由文件用于給不同的入口使用:web.php、api.php、 console.php 和 channels.php。 除此之外,我們還可以自定義路由文件。

這里介紹兩個比較重要的官方提供的默認路由文件web.php和api.php
1)web.php
文件包含的路由通過 RouteServiceProvider 引入,都被約束在 web 中間件組中,因而支持 Session、CSRF 保護以及 Cookie 加密功能,如果應用無需提供無狀態的、RESTful 風格的 API,那么路由基本上都要定義在 web.php 文件中
2)api.php
文件包含的路由通過 RouteServiceProvider 引入,都被約束在 api 中間件組中,因而支持頻率限制功能,這些路由是無狀態的,所以請求通過這些路由進入應用需要通過 token 進行認證并且不能訪問 Session 狀態。
2、路由定義

稍微復雜一點的情況:

3、RouteServiceProvider
文件包含的路由通過 RouteServiceProvider 引入

5、中間件
提到中間件,那一定離不開app/Http/Kernel.php這個文件
1) kernel
Kernel 中定義了重要的中間件列表,所有的請求 request 在被應用處理前,都必須經過這些中間件,篩過一遍后,才會被決定如何處理。這涉及到中間件(middleware)的作用。
App\Http\Kernel
?php
namespace App\Http;
use Illuminate\Foundation\Http\Kernel as HttpKernel;
class Kernel extends HttpKernel
{
/**
* The application's global HTTP middleware stack.
*
* These middleware are run during every request to your application.
*
* @var array
*/
protected $middleware = [
\App\Http\Middleware\CheckForMaintenanceMode::class,
\Illuminate\Foundation\Http\Middleware\ValidatePostSize::class,
\App\Http\Middleware\TrimStrings::class,
\Illuminate\Foundation\Http\Middleware\ConvertEmptyStringsToNull::class,
\App\Http\Middleware\TrustProxies::class,
\App\Http\Middleware\EnableCross::class,
];
/**
* The application's route middleware groups.
*
* @var array
*/
protected $middlewareGroups = [
'web' => [
\App\Http\Middleware\EncryptCookies::class,
\Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse::class,
\Illuminate\Session\Middleware\StartSession::class,
// \Illuminate\Session\Middleware\AuthenticateSession::class,
\Illuminate\View\Middleware\ShareErrorsFromSession::class,
\App\Http\Middleware\VerifyCsrfToken::class,
\Illuminate\Routing\Middleware\SubstituteBindings::class,
],
'api' => [
// 'throttle:300,1',
'bindings',
],
'web_api' => [
// 'throttle:300,1',
'bindings',
'check_token'
],
'admin_api' => [
// 'throttle:300,1',
'bindings',
'admin'
],
];
/**
* The application's route middleware.
*
* These middleware may be assigned to groups or used individually.
*
* @var array
*/
protected $routeMiddleware = [
'auth' => \Illuminate\Auth\Middleware\Authenticate::class,
'auth.basic' => \Illuminate\Auth\Middleware\AuthenticateWithBasicAuth::class,
'bindings' => \Illuminate\Routing\Middleware\SubstituteBindings::class,
'cache.headers' => \Illuminate\Http\Middleware\SetCacheHeaders::class,
'can' => \Illuminate\Auth\Middleware\Authorize::class,
'guest' => \App\Http\Middleware\RedirectIfAuthenticated::class,
'signed' => \Illuminate\Routing\Middleware\ValidateSignature::class,
'throttle' => \Illuminate\Routing\Middleware\ThrottleRequests::class,
'check_token' => \App\Http\Middleware\CheckToken::class,
];
}
上面的 $middleware[] 是面向全局的,特別是針對 HTTP 以及較為底層的。后面的 $middlewareGroups[] 和 $routeMiddleware[] 是比較具體的實施層面的。應該是可以根據開發需要繼續添加。
我們再看看App\Http\Kernel繼承的父類Illuminate\Foundation\Http\Kernel
?php
namespace Illuminate\Foundation\Http;
use Exception;
use Throwable;
use Illuminate\Routing\Router;
use Illuminate\Routing\Pipeline;
use Illuminate\Support\Facades\Facade;
use Illuminate\Contracts\Debug\ExceptionHandler;
use Illuminate\Contracts\Foundation\Application;
use Illuminate\Contracts\Http\Kernel as KernelContract;
use Symfony\Component\Debug\Exception\FatalThrowableError;
class Kernel implements KernelContract
{
/**
* The application implementation.
*
* @var \Illuminate\Contracts\Foundation\Application
*/
protected $app;
/**
* The router instance.
*
* @var \Illuminate\Routing\Router
*/
protected $router;
/**
* The bootstrap classes for the application.
* 引導類,起引導作用的類
* 這些類里面基本上都有一個 bootstrap(Application $app) 方法,
* 從不同的角度 bootstrap 應用。為最終 boot() 最準備。
* 注意:這些事做不完,不能接受請求,或許連$request都無法正確生成。
* @var array
*/
protected $bootstrappers = [
// 載入服務器環境變量(.env 文件?)
\Illuminate\Foundation\Bootstrap\LoadEnvironmentVariables::class,
// 載入配置信息(config 目錄?)
\Illuminate\Foundation\Bootstrap\LoadConfiguration::class,
// 配置如何處理異常
\Illuminate\Foundation\Bootstrap\HandleExceptions::class,
// 注冊 Facades
\Illuminate\Foundation\Bootstrap\RegisterFacades::class,
// 注冊 Providers
\Illuminate\Foundation\Bootstrap\RegisterProviders::class,
// 啟動 Providers
\Illuminate\Foundation\Bootstrap\BootProviders::class,
];
/**
* The application's middleware stack.
*
* @var array
*/
protected $middleware = [];
/**
* The application's route middleware groups.
*
* @var array
*/
protected $middlewareGroups = [];
/**
* The application's route middleware.
*
* @var array
*/
protected $routeMiddleware = [];
總之,Kernel 做了兩件事,第一個是定義 $bootstraps[],做好了 boot 系統的準備,第二個是定義 各種 middleware,這些都對 $request 進行加工、處理、甄選、判斷,最終為可以形成正確的、有效的 $response 做準備,都完成后,進行了 index.php 中的 $kernel->handle($request),返回 $response。
總結:
1) $request ---> $kernel { service providers/middlewares/routers } ---> $response
2) Kernel 是就是個大黑箱,送入請求,輸出響應,我們只管往里面添加服務、中間件、路由等等。
2) middleware

系統自帶的VerifyCsrfToken.php

自定義的中間件CheckToken.php
基本上中間件的具體過濾操作都在handle方法中完成

6、日志
1) 日志的配置文件:config/logging.php

2) logging.php

3) 使用參考
Log::channel('wechatlog')->info("獲取第三方平臺component_access_token",['data'=>$data]);
然后執行請求完畢,就可以在storage/logs這個文件夾下面看到對應的日志記錄

7、服務提供者
1)自定義服務提供者
在laravel里面,服務提供者其實就是一個工廠類。它最大的作用就是用來進行服務綁定。當我們需要綁定一個或多個服務的時候,可以自定義一個服務提供者,然后把服務綁定的邏輯都放在該類的實現中。在larave里面,要自定一個服務提供者非常容易,只要繼承Illuminate\Support\ServiceProvider這個類即可。
舉個栗子
app/providers/AppServiceProvider.php

在這個舉例里面,可以看到有一個register方法,這個方法是ServiceProvider里面定義的。自定義的時候,需要重寫它。這個方法就是用來綁定服務的。
2)laravel初始化自定義服務提供者的源碼

3)config/app.php
從上一步的源碼也能看到,laravel加載自定義服務提供者的時候,實際是從config/app.php這個配置文件里面的providers配置節找到所有要注冊的服務提供者的。

參考鏈接:https://blog.csdn.net/qqtaizi123/article/details/95949672