我有不同的用户组,例如admin,author,publisher,并分别为他们设置了控制器。我想在登录后根据以下内容设置默认路径:
$ this-> Auth-> User('group_id')
像这样在appcontroller中的beforefilter()方法中
if ($this->Auth->User('group_id') == '1')
{
Router::connect('/', array('controller' => 'admin', 'action' => 'index'));
}
elseif($this->Auth->User('group_id') == '2')
{
Router::connect('/', array('controller' => 'author', 'action' => 'index'));
}
else {
Router::connect('/', array('controller' => 'publisher', 'action' => 'index'));
}
我尝试了这个
routes.php
在配置中使用$ _SESSION变量,因为在该文件中无法使用$ this。主要目的是,当用户登录时,它将进入他们的控制器,因此我可以拥有一个干净的URL,我不想使用同一控制器,因此我不得不使用组检查功能,因为这将浪费ACL库的功能。任何帮助将不胜感激以实现这一目标。提前致谢
根据您对Isaac答案的评论,您可以执行以下操作。说,在routes.php中,您可以拥有:
Route::connect('/', array('controller' => 'some_controller', 'action' => 'index'));
然后在重定向控制器的index()
方法中:
public function index()
{
$group = $this->User->Group->find('first', array( //assuming User belongsTo Group
'conditions' => array(
'id' => $this->Auth->User('group_id')
),
'fields' => array('name')
)
)); //getting the name of the group the user belongs to
call_user_func(array($this, strtolower($group['Group']['name']).'_index'));
}
然后在您的控制器中,您可以看到以下内容:
protected function admin_index()
{
//code for admins
}
protected function publisher_index()
{
//code for publishers
}
protected function author_index()
{
//code for authors
}
因此,所有代码都在同一控制器上,但在不同方法上分开。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句