簡單的測試程式碼
需要在控制器中運行中間件,但它不起作用 另外,如果使用隨機密鑰更改中間件密鑰,則忽略它並回顯僅回顯“構造”字符串
<?php namespace App\Http\Controllers; use Closure; use Illuminate\Http\Request; class RootController extends Controller { public function __construct() { $this->middleware('middleware.key'); echo 'construct<br>'; } public function test(){ return 'test'; } }
<?php namespace App\Http\Middleware; use App\Http\Controllers\Controller; use Closure; use Illuminate\Http\Request; class TestMiddleware extends Controller { public function handle(Request $request, Closure $next) { echo 'middleware'; return $next($request); } }
output is : construct test
P粉6499901632024-02-26 10:50:46
您必須從 app/Http/Kernel.php
註冊中間件並將中間件新增至您的路由。
https://laravel.com/docs/9 .x/middleware#將中間件指派給路由