溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

laravel入門知識點大全

發(fā)布時間:2021-03-03 16:55:29 來源:億速云 閱讀:162 作者:TREX 欄目:開發(fā)技術(shù)

這篇文章主要講解了“l(fā)aravel入門知識點大全”,文中的講解內(nèi)容簡單清晰,易于學(xué)習(xí)與理解,下面請大家跟著小編的思路慢慢深入,一起來研究和學(xué)習(xí)“l(fā)aravel入門知識點大全”吧!

laravel入門

簡介

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

1、根目錄

laravel入門知識點大全

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

2、配置

laravel入門知識點大全

1)config目錄

該文件夾下面,包含的是各種配置文件。包括mysql數(shù)據(jù)庫連接信息,redis,自定義的配置文件信息等等

2).env文件

用以存儲一些依賴環(huán)境的變量,比如數(shù)據(jù)庫配置,因為它不會被加入到版本庫中, 所以還用以配置一些敏感信息:比如正式環(huán)境的一些第三方應(yīng)用賬號,token 等。有點類似Yii框架中的main-local.php

用法參考:env('DB_HOST','192.168.1.223')

說明:優(yōu)先使用.env文件中配置的DB_HOST對應(yīng)的值,如果.env中沒有配置,則使用這里設(shè)置的默認值'192.168.1.223'

laravel入門知識點大全

3)用法參考

config('redis_keys.redis_keys.all_follow_user')

3、MVC

laravel入門知識點大全

4、路由

1、routes目錄

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

laravel入門知識點大全

這里介紹兩個比較重要的官方提供的默認路由文件web.php和api.php

1)web.php

文件包含的路由通過 RouteServiceProvider 引入,都被約束在 web 中間件組中,因而支持 Session、CSRF 保護以及 Cookie 加密功能,如果應(yīng)用無需提供無狀態(tài)的、RESTful 風(fēng)格的 API,那么路由基本上都要定義在 web.php 文件中

2)api.php

文件包含的路由通過 RouteServiceProvider 引入,都被約束在 api 中間件組中,因而支持頻率限制功能,這些路由是無狀態(tài)的,所以請求通過這些路由進入應(yīng)用需要通過 token 進行認證并且不能訪問 Session 狀態(tài)。

2、路由定義

laravel入門知識點大全

稍微復(fù)雜一點的情況:

laravel入門知識點大全

3、RouteServiceProvider

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

laravel入門知識點大全

5、中間件

提到中間件,那一定離不開app/Http/Kernel.php這個文件

1) kernel

Kernel 中定義了重要的中間件列表,所有的請求 request 在被應(yīng)用處理前,都必須經(jīng)過這些中間件,篩過一遍后,才會被決定如何處理。這涉及到中間件(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[] 是比較具體的實施層面的。應(yīng)該是可以根據(jù)開發(fā)需要繼續(xù)添加。

我們再看看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.
  * 引導(dǎo)類,起引導(dǎo)作用的類
  * 這些類里面基本上都有一個 bootstrap(Application $app) 方法,
  * 從不同的角度 bootstrap 應(yīng)用。為最終 boot() 最準備。
  * 注意:這些事做不完,不能接受請求,或許連$request都無法正確生成。
  * @var array
  */
 protected $bootstrappers = [
  // 載入服務(wù)器環(huán)境變量(.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 系統(tǒng)的準備,第二個是定義 各種 middleware,這些都對 $request 進行加工、處理、甄選、判斷,最終為可以形成正確的、有效的 $response 做準備,都完成后,進行了 index.php 中的 $kernel->handle($request),返回 $response。

總結(jié):

1) $request ---> $kernel { service providers/middlewares/routers } ---> $response

2) Kernel 是就是個大黑箱,送入請求,輸出響應(yīng),我們只管往里面添加服務(wù)、中間件、路由等等。

2) middleware

laravel入門知識點大全

系統(tǒng)自帶的VerifyCsrfToken.php

laravel入門知識點大全

自定義的中間件CheckToken.php

基本上中間件的具體過濾操作都在handle方法中完成

laravel入門知識點大全

6、日志

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

laravel入門知識點大全

2) logging.php

laravel入門知識點大全

3) 使用參考

Log::channel('wechatlog')->info("獲取第三方平臺component_access_token",['data'=>$data]);

然后執(zhí)行請求完畢,就可以在storage/logs這個文件夾下面看到對應(yīng)的日志記錄

laravel入門知識點大全

7、服務(wù)提供者

1)自定義服務(wù)提供者

在laravel里面,服務(wù)提供者其實就是一個工廠類。它最大的作用就是用來進行服務(wù)綁定。當我們需要綁定一個或多個服務(wù)的時候,可以自定義一個服務(wù)提供者,然后把服務(wù)綁定的邏輯都放在該類的實現(xiàn)中。在larave里面,要自定一個服務(wù)提供者非常容易,只要繼承Illuminate\Support\ServiceProvider這個類即可。

舉個栗子

app/providers/AppServiceProvider.php

laravel入門知識點大全

在這個舉例里面,可以看到有一個register方法,這個方法是ServiceProvider里面定義的。自定義的時候,需要重寫它。這個方法就是用來綁定服務(wù)的。

2)laravel初始化自定義服務(wù)提供者的源碼

laravel入門知識點大全

3)config/app.php

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

laravel入門知識點大全

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

感謝各位的閱讀,以上就是“l(fā)aravel入門知識點大全”的內(nèi)容了,經(jīng)過本文的學(xué)習(xí)后,相信大家對laravel入門知識點大全這一問題有了更深刻的體會,具體使用情況還需要大家實踐驗證。這里是億速云,小編將為大家推送更多相關(guān)知識點的文章,歡迎關(guān)注!

向AI問一下細節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI