laravel middleware middleware

To create a new middleware, you can use make: middleware Artisan this command:

 
 

This command will set a name for CheckAge class in app / Http / Middleware directory. In this we are only allowed to request middleware older age variable is greater than 200 in order to access the route, otherwise, we will redirect the user to the Home "home" this URI.

 
 

As you can see, if age is less than 200, the middleware will return an HTTP redirect to the client, otherwise, the request will be further passed to the application. Next call the method with only $ $ request and passes the request to the application to the deeper (corresponding allows middleware).

HTTP request prior to the actual touching to the application, the layers may preferably through middleware. Each layer can be checked for a request, or even reject the request.



Registration Middleware #

Global Middleware #

If you want each HTTP request is passed through a middleware, $ middleware property inventory list as long as the middleware class added to the app / Http / Kernel.php in.

Routing assignment middleware #

If you want to assign to a specific route middleware, you have to be in app / Http / Kernel.php middleware to set up a friendly bond, by default, $ routeMiddleware property in the middle of this file already contains the current set of Laravel pieces, you only need to add a set of keys can be customized in the inventory list.

 
 

Once the middleware is defined in the HTTP kernel file, you can specify the use of middleware key in the routing options:

 
 

Specify multiple middleware routing:

 
 

You can use the full class name as the route assigned middleware.

 
 

Middleware group #

Sometimes you may want a way by specifying the key name will be assigned to a group of related middleware inside, making it easier to assign it to the route, which can be achieved by using the HTTP Kernel of $ middlewareGroups.

Laravel 自带了开箱即用的web和api两个中间件组以包含可以应用到 Web UI 和 API 路由的通用中间件:

 
 

中间件组可以被分配给路由和控制器动作,使用和单个中间件分配同样的语法。再次申明,中间件组的目的只是让一次分配给路由多个中间件的实现更加简单:

 
{tip} 默认情况下,RouteServiceProvider 已经为 routes.php 文件指定了 web 中间件组。


 

例子:

创建中间件

php artisan make:middleware AdminMiddleware

 

中间件编写登录验证

public function handle($request, Closure $next)
    {    
        
        if( !session()->get('userInfo') ){
            return redirect('admin/login');
        }
        return $next($request);
    }

 

注册中间件  (Kernel.php)

protected $middlewareGroups = [
        'web' => [
            \App\Http\Middleware\EncryptCookies::class,
            \Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse::class,
            \Illuminate\Session\Middleware\StartSession::class,
            // \Illuminate\Session\Middleware\AuthenticateSession::class,
            \Illuminate\View\Middleware\ShareErrorsFromSession::class,
            \App\Http\Middleware\VerifyCsrfToken::class,
            \Illuminate\Routing\Middleware\SubstituteBindings::class,
            \Illuminate\Session\Middleware\StartSession::class,
        ],

        'api' => [
            'throttle:60,1',
            'bindings',
        ],
    ];

 

 

使用:

<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\BaseController;
use Illuminate\Http\Request;
//use DB;
use Illuminate\Support\Facades\DB;
use App\Model\admin;

class IndexController extends BaseController
{
    public function __construct(){
        
        $this->middleware('admin.auth')->except(['login','signin']);
    }
}

 

Guess you like

Origin www.cnblogs.com/jasonLiu2018/p/11793455.html