您的位置 >>> 星想互联 >>> 编程技术 >>> PHP高级编程
laravel 中间件入门
点击数:299  发布时间2018-11-18 22:04:51

路由群组:

Route::group(['middleware'=>['check']],function(){
    Route::get('del',function(){
        echo "del";
    });
    
    Route::get('add',function(){
        echo "del";
    });
    
    Route::get('edit',function(){
        echo "del";
    });
});


中间件:Cke.php


namespace App\Http\Middleware;
use Closure;
class Cke
{
    /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure  $next
     * @return mixed
     */
    public function handle($request, Closure $next)
    {
        if(!request()->session()->has('name')){
            return redirect('list');
        }
        return $next($request);
    }
}


在app\Http\Kernel.php 中注册中间件:

protected $routeMiddleware = [
        'auth' => \App\Http\Middleware\Authenticate::class,
        'auth.basic' => \Illuminate\Auth\Middleware\AuthenticateWithBasicAuth::class,
        'can' => \Illuminate\Foundation\Http\Middleware\Authorize::class,
        'guest' => \App\Http\Middleware\RedirectIfAuthenticated::class,
        'throttle' => \Illuminate\Routing\Middleware\ThrottleRequests::class,
        'check' => \App\Http\Middleware\Cke::class,
    ];

来源:咸宁网站建设