Few days ago, I’m giving a training of CakePHP framework for AMDI-USM‘s Bio-ICT staff. We were developing one sample application and the default route will go to /users/login page. One of them ask me whether we can change the default routing after the user logged in.

  1. Before user login, the default routing will be /users/login
  2. After user logged in, default routing will go to /submissions/index

Nice question! Let’s try this and see if we could make it happen.

The Bootstrap

First of all, we need to do it in our /config/bootstrap.php, then we need to load SessionComponent to check whether user logged in to the system.

// include the Session Component to our application
App::uses('SessionComponent', 'Controller/Component');

// now create new SessionComponent instance
$Session = new SessionComponent(new ComponentCollection());

// check if the user logged in
if ($Session->read('Auth.User')) {

    switch ($Session->read('Auth.User.group_id'))
        // Administrator
        case 1:
            Configure::write('Route.default', array('controller' => 'pages', 'action' => 'dashboard', 'admin' => true));

        // Manager
        case 2:
            Configure::write('Route.default', array('controller' => 'pages', 'action' => 'dashboard', 'manager' => true));

        // Registered
        case 3:
            Configure::write('Route.default', array('controller' => 'pages', 'action' => 'dashboard'));
// nope, user not logged in
else {
    Configure::write('Route.default', array('controller' => 'pages', 'action' => 'display'));

Change the default routing

We need to change the default route in /Config/routes.php, set the default route to:

Router::connect('/', Configure::read('Route.default'));


That’s all you need to make it work. Simple and straight forward.

Link to Homepage

If you need a link to your homepage, normally in your website logo, you can use this code instead:

echo $this->Html->link('Home', Configure::read('Route.default'));

That’s all. Please let me know if it’s not working for you.