Kod ujian mudah
Perlu menjalankan perisian tengah dalam pengawal tetapi ia tidak berfungsi Selain itu, jika anda menukar kunci middleware dengan kunci rawak, abaikan ia dan bergema hanya rentetan "dibina"
<?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
Anda mesti mendaftarkan middleware daripada
app/Http/Kernel.php
dan menambah middleware pada laluan anda.https://laravel.com/docs/9 .x/middleware#Tugaskan middleware kepada laluan