dongyan7950 2016-12-28 08:58
浏览 95

将用户重定向到特定页面

I'm using Laravel 5.3 and Auth by default with this roles package. How can i do the normal user redirection after the user login if i have similar roles and also pages for them. For example i have AdminRole and after the Login i want to redirect user to /admin/dashboard.

I have tried something like this in the LoginController but it doesn't make sense:

protected function redirectTo()
{
    if (Auth::user()->isRole('admin'))
        return redirect()->route('admin');

    return redirect()->route('home');
}

Or maybe there is a better way to use middleware for redirecting?

Here is my routes (web.php):

Route::get('/', function () {
    return view('welcome');
});

Auth::routes();

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

Route::resource('company', 'CompanyController');

Route::group(['prefix' => 'admin'], function () {


    Route::get('login', function () {
        return view('admin.pages.admin-login');
    });


    Route::group(['middleware' => 'role:admin'], function () {
        Route::get('/', function () {
            return view('admin.admin-main');
        });
    });

});
  • 写回答

3条回答 默认 最新

  • doutan6286 2016-12-28 09:06
    关注

    use it like this way:

    return Redirect::to('admin');
    

    And note that:

    route:Route::get('company', 'CompanyController@show');

    controller: this works fine:

    function show(){
      return Redirect::to('home');
    }
    

    but this not

    function show(){
      $this->redirectto();
    }
    
    function redirectto()
    {
       return Redirect::to('home');
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥20 wireshark抓不到vlan
  • ¥20 关于#stm32#的问题:需要指导自动酸碱滴定仪的原理图程序代码及仿真
  • ¥20 设计一款异域新娘的视频相亲软件需要哪些技术支持
  • ¥15 stata安慰剂检验作图但是真实值不出现在图上
  • ¥15 c程序不知道为什么得不到结果
  • ¥40 复杂的限制性的商函数处理
  • ¥15 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来