laravel 5.2每次登录时如何调用中间件?

时间:2016-05-11 06:10:17

标签: php laravel laravel-5.2

我在5.0中有另一个laravel应用程序的中间件类,它将在用户登录时调用,我想在5.2中的另一个应用程序中使用,这里是类。

<?php namespace App\Http\Middleware;

use Closure;
use Illuminate\Contracts\Auth\Guard;
use Illuminate\Contracts\Routing\Middleware;
use Illuminate\Contracts\Routing\ResponseFactory;

use App\AssignedRoles;

class Admin implements Middleware {

    /**
     * The Guard implementation.
     *
     * @var Guard
     */
    protected $auth;

    /**
     * The response factory implementation.
     *
     * @var ResponseFactory
     */
    protected $response;

    /**
     * Create a new filter instance.
     *
     * @param  Guard  $auth
     * @param  ResponseFactory  $response
     * @return void
     */
    public function __construct(Guard $auth,
                                ResponseFactory $response)
    {
        $this->auth = $auth;
        $this->response = $response;
    }
    /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure  $next
     * @return mixed
     */
    public function handle($request, Closure $next)
    {
        if ($this->auth->check())
        {
            $admin = 0;
            $user_roles = AssignedRoles::join('roles','role_user.role_id','=','roles.id')
            ->where('user_id', $this->auth->user()->id)->select('roles.is_admin')->get();
            foreach($user_roles as $item)
            {
                if($item->is_admin==1)
                {
                    $admin=1;
                }
            }
            if($admin==0){
                return $this->response->redirectTo('/');
            }
            return $next($request);
        }
        return $this->response->redirectTo('/');
    }

}

我已经在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,
    'admin' => \App\Http\Middleware\Admin::class,   
    'age' => \App\Http\Middleware\Age::class,       
];

我在5.0中找到应用程序提示如何调用中间件但仍然不知道,这是路线:

Route::get('/', 'HomeController@index');
Route::get('home', 'HomeController@index');
Route::get('about', 'PagesController@about');
Route::get('contact', 'PagesController@contact');

Route::pattern('id', '[0-9]+');
Route::get('news/{id}', 'ArticlesController@show');
Route::get('video/{id}', 'VideoController@show');
Route::get('photo/{id}', 'PhotoController@show');

Route::controllers([
    'auth' => 'Auth\AuthController',
    'password' => 'Auth\PasswordController',
]);

if (Request::is('admin/*'))
{
    require __DIR__.'/admin_routes.php';
}

但我怎样才能在登录时每次都运行它?

2 个答案:

答案 0 :(得分:1)

试试这个:

Route::group(['middleware' => 'your-middleware'], function () {
    Route::post('/login', 'SiteController@login');
});

看,如果有帮助。

答案 1 :(得分:0)

试试这个

Route::get('page', ['middleware' => 'auth', urController@action]);