php - laravel filter group routes with same name -
i'm stuck annoying problem in laravel 4 project wich has 3 types of users being students, teachers , moderator (i use entrust role management solution).
each of them can browse same route, depending on user type, method should called. route.php files structured this:
route::group(array('before' => 'auth'), function(){ route::group(array('before' => 'teacher'), function(){ route::get('/tasks',array('as'=>'tasks','uses'=>'taskscontroller@tasksasteacher')); route::get('/task/{id}',array('as'=>'showtask','uses'=>'taskscontroller@showtaskasteacher')); }); route::group(array('before' => 'moderator'), function(){ route::get('/tasks',array('as'=>'tasks','uses'=>'taskscontroller@tasksasmoderator')); route::get('/task/{id}',array('as'=>'showtask','uses'=>'taskscontroller@showtaskasmoderator')); }); route::group(array('before' => 'student'), function(){ route::get('/tasks',array('as'=>'tasks','uses'=>'taskscontroller@tasksasstudent')); route::get('/task/{id}',array('as'=>'showtask','uses'=>'taskscontroller@showtaskasstudent')); }); });
however, browsing these routes teacher or moderator account returned 404 errors. found because routes redefined in 2 other filter groups.
so if redirect teacher user 'showtask', laravel return tasks route students,as that's last time 'showtask' route redefined, , 404 error.
my question was: best way handle error?
i hope isn't messy. in advance!
taking @matthias s's answer, work? instead of using entrust filter, check permissions route this:
//routes.php if(entrust::hasrole('teacher')) { route::get('/tasks',array('as'=>'tasks','uses'=>'taskscontroller@tasksasteacher')); route::get('/task/{id}',array('as'=>'showtask','uses'=>'taskscontroller@showtaskasteacher')); }
repeat different roles
edit: if had role of user stored in session, use sort of automatic route this:
//routes.php if(entrust::hasrole(session::get('role'))) { route::get('/tasks',array('as'=>'tasks','uses'=>'taskscontroller@tasksas'.session::get('role'))); route::get('/task/{id}',array('as'=>'showtask','uses'=>'taskscontroller@showtaskas'.session::get('role'))); }
this way can add many roles want once add correct controller function role.
edit #2:
or guess better
//routes.php - updated, verify role inside controller instead of defining routes based on role route::get('/tasks',array('as'=>'tasks','uses'=>'taskscontroller@tasks')); route::get('/task/{id}',array('as'=>'showtask','uses'=>'taskscontroller@showtask')); //taskscontroller.php public function __construct(){ if(!session::get('role')){ //make sure user has role assigned return redirect::to('login'); // redirect login or permission screen if not } } public function tasks(){ if(entrust::hasrole(session::get('role')){ $tasks = tasks::where('role_id', '=', $role->id); // tasks based on role view::make('tasks.index')->with('tasks', $tasks); } else{ // show permissions error user } } public function showtask($task_id){ if(entrust::hasrole(session::get('role')){ $task = tasks::where('role_id', '=', $role->id)->where('id', '=', $task_id)->first(); view::make('tasks.view')->with('task', $task); } }
Comments
Post a Comment