# 6 - Laravel 5.2 Middleware 中面層

https://laravel.tw/docs/5.0/middleware

HTTP 中介層提供一個方便的機制來過濾進入應用程式的 HTTP 請求

生成php artisan make:middleware OldMiddleware

如果student_no不是s8183758441 返回上一頁

app/Http/Middleware/OldMiddleware.php

<?php

namespace App\Http\Middleware;

use Closure;

class OldMiddleware
{
    /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure  $next
     * @return mixed
     */
    public function handle($request, Closure $next)
    {
        if ($request->student_no != 's8183758441') {
            return redirect()->back();//上一頁
        }
        return $next($request);
    }
}

app/Http/Kernel.php

註冊OldMiddleware

    protected $routeMiddleware = [
        ...
        'test' => \App\Http\Middleware\OldMiddleware::class,
    ];

app/Http/routes.php

...
Route::group(['middleware'=>'test'],function(){
    Route::get('edit/{student_no}','[email protected]');
    Route::post('edit/{student_no}','[email protected]');
});

效果: 如果student_no不是s8183758441 返回上一頁

Last Updated: Sun Aug 11 2019 13:25:40 GMT+0000
贊助商連結
(adsbygoogle = window.adsbygoogle || []).push({});