php - Laravel 5 Entrust one route - load different controller based on Role -


so i'm starting learn laravel , i've implemented entrust role permission package, works well. question is, i'd have 'dashboard' page so: example.com/dashboard.

the thing is, i'm not sure how set up. since in app\http\controllers folder made subfolders admin , user, both have dashboardcontroller since want show different data either type of user.

how can declare route point dashboard , check role authenticated user has, loads correct controller? have namespaces? haven't found answer yet

trying more clear, don't want have this: example.com/dashboard/admin , example.com/dashboard/user, rather 1 url example.com/dashboard , check role user has.

i'm sorry if answer obvious, stuff new me , haven't found answer yet.

something should work 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 this approach might 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, feel having dashboardcontroller works better. find when using dashboard, pull variety of models opening page statistics site. use dashboardcontroller general information. dashboard, other pages have specific items want view/edit. way if admins need access view/edit same information users, not need rewrite code. can update security on particular controller allow both groups.


Comments

Popular posts from this blog

Java 3D LWJGL collision -

spring - SubProtocolWebSocketHandler - No handlers -

methods - python can't use function in submodule -