php 中间件,检查 Laravel 5 后如何重定向

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/29062680/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 00:13:23  来源:igfitidea点击:

Middleware, how to redirect after check Laravel 5

phpredirectrouteslaravel-5

提问by Vladimir Djukic

I need after check if user is logged as editor, to redirect to profile page...

我需要检查用户是否以编辑者身份登录后,重定向到个人资料页面...

Here is my code:

这是我的代码:

<?php namespace App\Http\Middleware;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Redirect;

use Closure;

class AdminMiddleware {

    /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure  $next
     * @return mixed
     */
    public function handle($request, Closure $next)
    {
        if(Auth::check()){
                    if(Auth::user()->roles->toArray()[0]['role'] == 'editor'){
                       return redirect('/profile');
                    }
                    return $next($request);
                }
                else
                {

                   return $next($request);
                }
    }

}

Problem with this code is when user is editor I get infinite loop....

这段代码的问题是当用户是编辑器时,我得到无限循环....

Here is my routs:

这是我的路线:

Route::group(['middleware' => 'auth'], function(){

    Route::get('home', ['middleware' => 'admin', function()
        {
        return view('home');
        }]);
    Route::get('profile', array(
       'as' => 'profile',
       'uses' => 'UserController@getProfile'
    ));


});

Anyone know what is problem?

有谁知道是什么问题?

采纳答案by Emeka Mbah

Where did you register your middleware in App\Http\Kernel?

你在哪里注册你的中间件App\Http\Kernel

Is it in protected $middleware = []or protected $routeMiddleware = []?

它是受保护的$middleware = []还是protected $routeMiddleware = []

If registered in $middlewareit will run on each very request thereby causing infinite loop, if so use only $routeMiddleware

如果注册,$middleware它将在每个请求上运行从而导致无限循环,如果是这样,只使用$routeMiddleware

回答by Harry Bosh

I found this to be a less code and less decisions for redirecting users based on roles, Put this in your AuthController.php

我发现这是一个更少的代码和更少的基于角色重定向用户的决定,把它放在你的 AuthController.php 中

protected function authenticated( $user)
{

    if($user->user_group == '0') {
        return redirect('/dashboard');
    }

    return redirect('my-account');
}

https://laracasts.com/discuss/channels/laravel/how-best-to-redirect-admins-from-users-after-login-authentication

https://laracasts.com/discuss/channels/laravel/how-best-to-redirect-admins-from-users-after-login-authentication

回答by Koushik Das

Go to Kernel.php. It's in app\http. Try to find protected $routeMiddlewarein that array you need to add this

Kernel.php。它在app\http. 尝试protected $routeMiddleware在该数组中找到 您需要添加的

'admin' => \App\Http\Middleware\AdminMiddleware::class

After that it should work fine. Hope this helps anyone who faces the same problem.

之后它应该可以正常工作。希望这可以帮助任何面临同样问题的人。