laravel入門
簡(jiǎn)介
作為PHP最常用的框架之一,Laravel的框架目錄布置得尤其清晰,適用于各種類型的項(xiàng)目開發(fā)。今天來記錄下laravel入門需要熟悉的知識(shí)點(diǎn)。
1、根目錄
![](/d/20211017/d120e131ae254c315f53e32beaa0f5be.gif)
其中,public/index.php是項(xiàng)目的入口文件
2、配置
![](/d/20211017/db37afc8bf01c61ac6b70b6d23f4cea1.gif)
1)config目錄
該文件夾下面,包含的是各種配置文件。包括mysql數(shù)據(jù)庫(kù)連接信息,redis,自定義的配置文件信息等等
2).env文件
用以存儲(chǔ)一些依賴環(huán)境的變量,比如數(shù)據(jù)庫(kù)配置,因?yàn)樗粫?huì)被加入到版本庫(kù)中, 所以還用以配置一些敏感信息:比如正式環(huán)境的一些第三方應(yīng)用賬號(hào),token 等。有點(diǎn)類似Yii框架中的main-local.php
用法參考:env('DB_HOST','192.168.1.223')
說明:優(yōu)先使用.env文件中配置的DB_HOST對(duì)應(yīng)的值,如果.env中沒有配置,則使用這里設(shè)置的默認(rèn)值'192.168.1.223'
![](/d/20211017/d91c6e7c1136ba0123bfd11910aad346.gif)
3)用法參考
config('redis_keys.redis_keys.all_follow_user')
3、MVC
![](/d/20211017/fb476b57a918c7d96a950f5d96257b70.gif)
4、路由
1、routes目錄
routes目錄包含了應(yīng)用定義的所有路由。Laravel 默認(rèn)提供了四個(gè)路由文件用于給不同的入口使用:web.php、api.php、 console.php 和 channels.php。 除此之外,我們還可以自定義路由文件。
![](/d/20211017/0b87a5c9ce96d537054e1d5938a4464a.gif)
這里介紹兩個(gè)比較重要的官方提供的默認(rèn)路由文件web.php和api.php
1)web.php
文件包含的路由通過 RouteServiceProvider 引入,都被約束在 web 中間件組中,因而支持 Session、CSRF 保護(hù)以及 Cookie 加密功能,如果應(yīng)用無(wú)需提供無(wú)狀態(tài)的、RESTful 風(fēng)格的 API,那么路由基本上都要定義在 web.php 文件中
2)api.php
文件包含的路由通過 RouteServiceProvider 引入,都被約束在 api 中間件組中,因而支持頻率限制功能,這些路由是無(wú)狀態(tài)的,所以請(qǐng)求通過這些路由進(jìn)入應(yīng)用需要通過 token 進(jìn)行認(rèn)證并且不能訪問 Session 狀態(tài)。
2、路由定義
![](/d/20211017/e4bd2567e42dba1e15bcebca629285bb.gif)
稍微復(fù)雜一點(diǎn)的情況:
![](/d/20211017/9d5b0a630bc781a0fcf2f3b4b66c0c31.gif)
3、RouteServiceProvider
文件包含的路由通過 RouteServiceProvider 引入
![](/d/20211017/253b882830b2a8d935ee57c6f24a4c6f.gif)
5、中間件
提到中間件,那一定離不開app/Http/Kernel.php這個(gè)文件
1) kernel
Kernel 中定義了重要的中間件列表,所有的請(qǐng)求 request 在被應(yīng)用處理前,都必須經(jīng)過這些中間件,篩過一遍后,才會(huì)被決定如何處理。這涉及到中間件(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[] 是面向全局的,特別是針對(duì) HTTP 以及較為底層的。后面的 $middlewareGroups[] 和 $routeMiddleware[] 是比較具體的實(shí)施層面的。應(yīng)該是可以根據(jù)開發(fā)需要繼續(xù)添加。
我們?cè)倏纯碅pp\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.
* 引導(dǎo)類,起引導(dǎo)作用的類
* 這些類里面基本上都有一個(gè) bootstrap(Application $app) 方法,
* 從不同的角度 bootstrap 應(yīng)用。為最終 boot() 最準(zhǔn)備。
* 注意:這些事做不完,不能接受請(qǐng)求,或許連$request都無(wú)法正確生成。
* @var array
*/
protected $bootstrappers = [
// 載入服務(wù)器環(huán)境變量(.env 文件?)
\Illuminate\Foundation\Bootstrap\LoadEnvironmentVariables::class,
// 載入配置信息(config 目錄?)
\Illuminate\Foundation\Bootstrap\LoadConfiguration::class,
// 配置如何處理異常
\Illuminate\Foundation\Bootstrap\HandleExceptions::class,
// 注冊(cè) Facades
\Illuminate\Foundation\Bootstrap\RegisterFacades::class,
// 注冊(cè) Providers
\Illuminate\Foundation\Bootstrap\RegisterProviders::class,
// 啟動(dòng) 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 做了兩件事,第一個(gè)是定義 $bootstraps[],做好了 boot 系統(tǒng)的準(zhǔn)備,第二個(gè)是定義 各種 middleware,這些都對(duì) $request 進(jìn)行加工、處理、甄選、判斷,最終為可以形成正確的、有效的 $response 做準(zhǔn)備,都完成后,進(jìn)行了 index.php 中的 $kernel->handle($request),返回 $response。
總結(jié):
1) $request ---> $kernel { service providers/middlewares/routers } ---> $response
2) Kernel 是就是個(gè)大黑箱,送入請(qǐng)求,輸出響應(yīng),我們只管往里面添加服務(wù)、中間件、路由等等。
2) middleware
![](/d/20211017/634fdf302020f948244ab49c8bd42c50.gif)
系統(tǒng)自帶的VerifyCsrfToken.php
![](/d/20211017/22ae150005c8873b6d8c822b321faaef.gif)
自定義的中間件CheckToken.php
基本上中間件的具體過濾操作都在handle方法中完成
![](/d/20211017/12c8c6219defd42ba2f1567b5425d2a9.gif)
6、日志
1) 日志的配置文件:config/logging.php
![](/d/20211017/e03a44660742e5b7e115149f59e71816.gif)
2) logging.php
![](/d/20211017/77cc96b9628ab01af2531c8fdd069951.gif)
3) 使用參考
Log::channel('wechatlog')->info("獲取第三方平臺(tái)component_access_token",['data'=>$data]);
然后執(zhí)行請(qǐng)求完畢,就可以在storage/logs這個(gè)文件夾下面看到對(duì)應(yīng)的日志記錄
![](/d/20211017/896e58c536f01e30425442322332c2bd.gif)
7、服務(wù)提供者
1)自定義服務(wù)提供者
在laravel里面,服務(wù)提供者其實(shí)就是一個(gè)工廠類。它最大的作用就是用來進(jìn)行服務(wù)綁定。當(dāng)我們需要綁定一個(gè)或多個(gè)服務(wù)的時(shí)候,可以自定義一個(gè)服務(wù)提供者,然后把服務(wù)綁定的邏輯都放在該類的實(shí)現(xiàn)中。在larave里面,要自定一個(gè)服務(wù)提供者非常容易,只要繼承Illuminate\Support\ServiceProvider這個(gè)類即可。
舉個(gè)栗子
app/providers/AppServiceProvider.php
![](/d/20211017/2be0cac313aa616740f6325b181f507e.gif)
在這個(gè)舉例里面,可以看到有一個(gè)register方法,這個(gè)方法是ServiceProvider里面定義的。自定義的時(shí)候,需要重寫它。這個(gè)方法就是用來綁定服務(wù)的。
2)laravel初始化自定義服務(wù)提供者的源碼
![](/d/20211017/777c6554a0e75ad684d98a9ad74d2c33.gif)
3)config/app.php
從上一步的源碼也能看到,laravel加載自定義服務(wù)提供者的時(shí)候,實(shí)際是從config/app.php這個(gè)配置文件里面的providers配置節(jié)找到所有要注冊(cè)的服務(wù)提供者的。
![](/d/20211017/3d4f4cf71d77ab7d70fb760e81609a75.gif)
參考鏈接:https://blog.csdn.net/qqtaizi123/article/details/95949672