laravel的中间件demo

过滤器已经被废除。。。刚学才两天,蛋疼

 

创建一个中间件

./artisan make:middleware TestMiddleware

 

大概代码

<?php namespace App\Http\Middleware;

use Closure;

class TestMiddleware {

    /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure  $next
     * @return mixed
     */
    public function handle($request, Closure $next)
    {
        if ($request->input('age') < 200)
        {
            return redirect('home');
        }

        return $next($request);
    }

}

 

在route里面注册

kernel.php

protected $routeMiddleware = [
        'auth' => 'App\Http\Middleware\Authenticate',
        'auth.basic' => 'Illuminate\Auth\Middleware\AuthenticateWithBasicAuth',
        'guest' => 'App\Http\Middleware\RedirectIfAuthenticated',
        
         //这里注册
        'test' => 'App\Http\Middleware\TestMiddleware',
    ];

 

Route::get('/', ['middleware' => 'test',function()
{
    $value = [1,2,3];
    return View::make('home.index', array('data' => $value));
}]);

 

自己测试下吧

你可能感兴趣的:(laravel)