Cambiar la base de acciones predeterminada en grupos de usuarios en CakePHP

I have different users groups e.g. admin, author,publisher and have separately controllers for them I want to set default path after login on base of


like this in appcontroller in beforefilter() method

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'));

Probé esto en


in Config using $_SESSION Variable because in that file couldnt use $this. Main purpose is that when user login it will take to their controller and so i can have clean URL I dont want to same controller i have to use check for group than ACL library's power will be in waste. Any Help will be appreciated to accomplish this. Thanks in advance

preguntado el 28 de mayo de 14 a las 14:05

2 Respuestas

Based on your comment on Isaac's answer, you can do something like this. Say, in routes.php you can have:

Route::connect('/', array('controller' => 'some_controller', 'action' => 'index'));

Then in your redirected controller's index() método:

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'));


And then in your controller you can have something like:

protected function admin_index()
    //code for admins

protected function publisher_index()
    //code for publishers

protected function author_index()
    //code for authors

So you have all code on the same controller but separated on different methods.

contestado el 28 de mayo de 14 a las 15:05

It seemed best approach till now but is there possibility Route::connect('/' I can change default controller for each user group ? - fahad abdullah

//Don't miss the app_controller for this small kind of thing. bear in mind always keep the app controller clean and tidy.

function login(){
    //here you can say after login what's happen next
    if ($this->Auth->login()){


//redirect user groups
function redirectUser(){
    $role = $this->Auth->User('group_id');

    switch ($role) {
        case 1:
              return $this->redirect(array('controllers'=>'admin_controller', 'action'=>'action_name')); 
        case 2:
              return $this->redirect(array('controllers'=>'author_controller', 'action'=>'action_name'));
              return $this->redirect(array('controllers'=>'user_controller', 'action'=>'action_name'));

If you want to use custom url You also need to name it into routes.php

Route::connect('/admin', array('controller' => 'admin_controller', 'action' => 'index'));

the rest of links

contestado el 28 de mayo de 14 a las 17:05

It will get job done but my URL will be like this eg. , etc I dont want to show each user group controller name in URL after login , How can I achieve this. - fahad abdullah

first make your Routes in Route file and then follow this - Furia

Can I change default controller for each user group after login.. ? - fahad abdullah

sure u can. by doing this you can tell to each type of user. after login which page they will be redirect to - Furia

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.