dounuo8797 2015-05-29 13:25
浏览 34
已采纳

Laravel 5委托一条路线 - 根据角色加载不同的控制器

So I'm just starting to learn Laravel and I've implemented the Entrust Role Permission package, which works really well. Now my question is, I'd like to have a 'Dashboard' page like so: example.com/dashboard.

The thing is, I'm not sure how to set this up. Since In my App\Http\Controllers folder I made subfolders for Admin and User, they both have a Dashboardcontroller since I want to show different data for either type of user.

How can I declare a route that would point to Dashboard and check which Role the authenticated user has, then loads the correct controller? Does it have to do something with namespaces? I haven't really found a good answer yet

So trying to be more clear, I don't want to have to do this: example.com/dashboard/admin and example.com/dashboard/user, rather just one url example.com/dashboard and check which role the user has.

I'm sorry if the answer is really obvious, this stuff is new to me and I haven't found any good answer yet.

  • 写回答

1条回答 默认 最新

  • dongyishe6689 2015-05-29 16:33
    关注

    Something like this should work for you.

    routes.php

    Route::get('/dashboard', ['as'=>'dashboard', 'uses'=> 'DashboardController@dashboard']);
    

    DashboardController.php

    use App\Http\Requests;
    use App\Http\Controllers\Controller;
    
    use App\User;
    use App\Admin;
    
    class DashboardController extends Controller {
        public function dashboard(){
            if ($this->auth->user()->hasRole('admin') )
            {
                return $this->showAdminDashboard();
            }
            elseif ($this->auth->user()->hasRole('user') )
            {
                return $this->showUserDashboard();
            }
            else {
                abort(403);
            }
        }
    
        private function showUserDashboard()
        {
            return view('dashboard.user');
        }
    
        private function showAdminDashboard()
        {
            return view('dashboard.admin');
        }
    }
    

    Edits per comment

    It sounds like something like this approach might be what you are looking for.

    <?php
    
    $uses = 'PagesController@notfound';
    if( $this->auth->user()->hasRole('admin') )
    {
        $uses = 'AdminController@index';
    }
    elseif ($this->auth->user()->hasRole('user') )
    {
        $uses = 'UsersController@index';
    }
    
    
    Route::get('/dashboard', array(
         'as'=>'home'
        ,'uses'=> $uses
    ));
    

    That said, I feel like having a DashboardController works better. I find when using a dashboard, I pull from a variety of models for the opening page to get statistics about the site. I would use the DashboardController only for this general information. From the Dashboard, I would like to other pages that have the specific items you want to view/edit. This way if the admins need to access view/edit the same information as the users, you do not need to rewrite code. You can just update the security on that particular controller to allow for both groups.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 #MATLAB仿真#车辆换道路径规划
  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建
  • ¥15 数据可视化Python
  • ¥15 要给毕业设计添加扫码登录的功能!!有偿
  • ¥15 kafka 分区副本增加会导致消息丢失或者不可用吗?
  • ¥15 微信公众号自制会员卡没有收款渠道啊
  • ¥15 stable diffusion
  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条