开发者

Kohana multiple default controllers based on roles

I want 开发者_开发知识库to display completely different layouts for users in different roles on the root url of my application. I am currently achieving this using the following lines in bootstrap.php.

if (Auth::instance()->logged_in()){
  Route::set('default', '(<controller>(/<action>(/<id>)))')
   ->defaults(array('controller' => 'profile','action'     => 'index',));
}    
else{ 
 Route::set('default', '(<controller>(/<action>(/<id>)))')
  ->defaults(array('controller' => 'welcome','action'     => 'index',));
}

What is the best practice to achieve this in Kohana? Is it ok to add more lines for different roles in bootstrap.php.

Thanks


you should consider using lambdacallback-route-logic

If allows you to modify the requested URL dynamically and much more cleaner than writing something in bootstrap.php


Why dont change basic template in ONE controller (and using the same route)? I think, your controller code doesn't differs if user logged in or not.


I do it like this: Create an abstract class Controller_Rolebased where in before() method you can implement Role checking. And then for example:

class Controller_Profile extends Controller_Rolebased
{
    protected $_accept_roles = array('user', 'admin'); // this array Controller_Rolebased class will use in before method.
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜