首頁  >  文章  >  php框架  >  thinkphp中介軟體是什麼

thinkphp中介軟體是什麼

藏色散人
藏色散人原創
2019-07-04 13:30:347729瀏覽

ThinkPHP是為了簡化企業級應用開發和敏捷WEB應用開發而誕生的。最早誕生於2006年初,2007年元旦正式更名為ThinkPHP,並遵循Apache2開源協定發布。 ThinkPHP從誕生以來一直秉承簡潔實用的設計原則,在保持出色的性能和至簡的程式碼的同時,也注重易用性。並且擁有眾多原創功能和特性,在社區團隊的積極參與下,在易用性、擴展性和性能方面不斷優化和改進。

thinkphp中介軟體是什麼

thinkphp中間件是什麼?

從5.1.6 版本開始,正式引入中間件的支援。

中間件主要用於攔截或過濾應用的HTTP請求,並進行必要的業務處理。

定義中間件

可以透過命令列指令快速產生中間件

php think make:middleware Check

這個指令會application/http/middleware目錄下面產生一個Check中介軟體.

<?php
namespace app\http\middleware;
class Check
{
    public function handle($request, \Closure $next)
    {
        if ($request->param(&#39;name&#39;) == &#39;think&#39;) {
            return redirect(&#39;index/think&#39;);
        }
        return $next($request);
    }
}

中間件的入口執行方法必須是handle方法,而且第一個參數是Request對象,第二個參數是一個閉包。

中間件handle方法的回傳值必須是一個Response物件。

在這個中間件中我們判斷目前請求的name參數等於think的時候進行重定向處理。否則,請求將進一步傳遞到應用程式中。要讓請求繼續傳遞到應用程式中,只需使用 $request 作為參數去呼叫回調函數 $next 。

在某些需求下,可以使用第三個參數傳入額外的參數。

<?php
namespace app\http\middleware;
class Check
{
    public function handle($request, \Closure $next, $name)
    {
        if ($name == &#39;think&#39;) {
            return redirect(&#39;index/think&#39;);
        }
        return $next($request);
    }
}

前置/後置中間件

中間件是在請求特定的操作之前還是之後執行,完全取決於中間件的定義本身。

下面是一個前置行為的中間件

<?php
namespace app\http\middleware;
class Before
{
    public function handle($request, \Closure $next)
    {
        // 添加中间件执行代码
        return $next($request);
    }
}

下面是一個後置行為的中間件

<?php
namespace app\http\middleware;
class After
{
    public function handle($request, \Closure $next)
    {
$response = $next($request);
        // 添加中间件执行代码
        return $response;
    }
}

來個比較實際的例子,我們需要判斷當前瀏覽器環境是在微信或支付寶

namespace app\http\middleware;
/**
 * 访问环境检查,是否是微信或支付宝等
 */
class InAppCheck
{
    public function handle($request, \Closure $next)
    {
        if (preg_match(&#39;~micromessenger~i&#39;, $request->header(&#39;user-agent&#39;))) {
            $request->InApp = &#39;WeChat&#39;;
        } else if (preg_match(&#39;~alipay~i&#39;, $request->header(&#39;user-agent&#39;))) {
            $request->InApp = &#39;Alipay&#39;;
        }
        return $next($request);
    }
}

然後在你的行動版的module裡加入一個middleware.php檔案

例如:/path/application/mobile/middleware.php

return [
    app\http\middleware\InAppCheck::class,
];

然後在你的controller中可以透過$this->request->InApp取得相關的值

註冊中間件

路由中間件

最常用的中間件註冊方式是註冊路由中間件

Route::rule(&#39;hello/:name&#39;,&#39;hello&#39;)
->middleware(&#39;Auth&#39;);

或使用完整的中間件類別名稱

Route::rule(&#39;hello/:name&#39;,&#39;hello&#39;)
->middleware(app\http\middleware\Auth::class);

支援註冊多個中介軟體

Route::rule(&#39;hello/:name&#39;,&#39;hello&#39;)
->middleware([&#39;Auth&#39;, &#39;Check&#39;]);

V5.1.7 版本,可以直接在應用程式設定目錄下的middleware.php中先預先定義中間件(其實就是增加別名識別),例如:

return [
&#39;auth&#39;=>app\http\middleware\Auth::class,
    &#39;check&#39;=>app\http\middleware\Check::class
];

然後直接在路由中使用中間件別名註冊

Route::rule(&#39;hello/:name&#39;,&#39;hello&#39;)
->middleware([&#39;Auth&#39;, &#39;Check&#39;]);

V5.1.8 版本開始,可以支援使用別名定義一組中間件,例如:

return [
&#39;check&#39;=>[
    app\http\middleware\Auth::class,
   app\http\middleware\Check::class
    ],
];

然後,直接使用下面的方式註冊中間件

Route::rule(&#39;hello/:name&#39;,&#39;hello&#39;)
->middleware(&#39;check&#39;);

支援對路由分組註冊中間件

Route::group(&#39;hello&#39;, function(){
Route::rule(&#39;hello/:name&#39;,&#39;hello&#39;);
})->middleware(&#39;Auth&#39;);

V5.1.8 版本開始支援對某個網域註冊中間件

Route::domain(&#39;admin&#39;, function(){
// 注册域名下的路由规则
})->middleware(&#39;Auth&#39;);

如果需要傳入額外參數給中間件,可以使用

Route::rule(&#39;hello/:name&#39;,&#39;hello&#39;)
->middleware(&#39;Auth:admin&#39;);

如果使用的是常數方式定義,可以在第二個參數傳入中間件參數。

Route::rule(&#39;hello/:name&#39;,&#39;hello&#39;)
->middleware(Auth::class, &#39;admin&#39;);

如果需要定義多個中間件,使用陣列方式

Route::rule(&#39;hello/:name&#39;,&#39;hello&#39;)
->middleware([Auth::class, &#39;Check&#39;]);

可以統一傳入同一個額外參數

Route::rule(&#39;hello/:name&#39;,&#39;hello&#39;)
->middleware([Auth::class, &#39;Check&#39;], &#39;admin&#39;);

或單獨指定中間件參數。

Route::rule(&#39;hello/:name&#39;,&#39;hello&#39;)
->middleware([&#39;Auth:admin&#39;, &#39;Check:editor&#39;]);

使用閉包定義中間件

你不一定要使用中間件類,在某些簡單的場合你可以使用閉包定義中間件,但閉包函數必須傳回Response物件實例。

Route::group(&#39;hello&#39;, function(){
Route::rule(&#39;hello/:name&#39;,&#39;hello&#39;);
})->middleware(function($request,\Closure $next){
    if ($request->param(&#39;name&#39;) == &#39;think&#39;) {
        return redirect(&#39;index/think&#39;);
    }
    
return $next($request);
});

全域中間件

你可以在應用程式目錄下方定義middleware.php文件,使用下面的方式:

<?php
return [
\app\http\middleware\Auth::class,
    &#39;Check&#39;,
    &#39;Hello&#39;,
];

中間件的註冊應該使用完整的類別名,如果沒有指定命名空間則使用app\http\middleware作為命名空間。

全域中間件的執行順序就是定義順序。可以在定義全域中間件的時候傳入中間件參數,支援兩種方式傳入。

<?php
return [
[\app\http\middleware\Auth::class, &#39;admin&#39;],
    &#39;Check&#39;,
    &#39;Hello:thinkphp&#39;,
];

上面的定義表示 給Auth中間件傳入admin參數,給Hello中間件傳入thinkphp參數。

模組中間件

V5.1.8 版本開始,支援模組中間件定義,你可以直接在模組目錄下方增加middleware.php文件,定義方式和應用中間件定義一樣,只是只會在這個模組下面生效。

控制器中間件

V5.1.17 版本開始,支援為控制器定義中間件。首先你的控制器需要繼承系統的think\Controller類,然後在控制器中定義middleware屬性,例如:

<?php
namespace app\index\controller;
use think\Controller;
class Index extends Controller
{
    protected $middleware = [&#39;Auth&#39;];
    public function index()
    {
        return &#39;index&#39;;
    }
    public function hello()
    {
        return &#39;hello&#39;;
    }
}

當執行index控制器的時候就會呼叫Auth中間件,一樣支援使用完整的命名空間定義。

如果需要設定控制器中間的生效操作,可以如下定義:

<?php
namespace app\index\controller;
use think\Controller;
class Index extends Controller
{
    protected $middleware = [ 
    &#39;Auth&#39; => [&#39;except&#39; => [&#39;hello&#39;] ],
        &#39;Hello&#39; => [&#39;only&#39; => [&#39;hello&#39;] ],
    ];
    public function index()
    {
        return &#39;index&#39;;
    }
    public function hello()
    {
        return &#39;hello&#39;;
    }
}

中間件向控制器傳參

可以透過給予請求物件賦值的方式傳參給控制器(或其它地方),例如

<?php
namespace app\http\middleware;
class Hello
{
    public function handle($request, \Closure $next)
    {
        $request->hello = &#39;ThinkPHP&#39;;
        
        return $next($request);
    }
}

注意,傳遞的變數名稱不要和param變數有衝突。

然後在控制器的方法裡面可以直接使用

public function index(Request $request)
{
return $request->hello; // ThinkPHP
}

本文来自ThinkPHP框架技术文章栏目:http://www.php.cn/phpkj/thinkphp/

以上是thinkphp中介軟體是什麼的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn