laravel中间件学习笔记

路由中间件:

1,创建中间件

php artisan make:middleware CheckLogin

2,注册中间件

// Within App\Http\Kernel Class...

protected $routeMiddleware = [

    'auth' => \Illuminate\Auth\Middleware\Authenticate::class,

    'auth.basic' => \Illuminate\Auth\Middleware\AuthenticateWithBasicAuth::class,

    'bindings' => \Illuminate\Routing\Middleware\SubstituteBindings::class,

    'can' => \Illuminate\Auth\Middleware\Authorize::class,

    'guest' => \App\Http\Middleware\RedirectIfAuthenticated::class,

    'throttle' => \Illuminate\Routing\Middleware\ThrottleRequests::class,

];


3,使用中间件

Route::group(['middleware' => 'check.login'],function(){

        Route::get('tocart','view\cartcontroller@tocart');

        Route::get('order_commit/{product_ids}','view\ordercontroller@toOrderCommit');

        Route::get('toOrderList','view\ordercontroller@toOrderList');

    });

控制器中间件

public function __construct()

    {

        $this->middleware('auth',[

            'except'=>['show','create','store']

        ]);

    }

你可能感兴趣的:(laravel中间件学习笔记)