phpcakephpcakephp-routing

Cake PHP Routing issue in 2.0Beta


I'm upgrading a dev site to Cake 2.0 Beta and my custom routing appears to be broken. The previous site routed the url /login to the Employees controller, action:login. This is the code setting up the routes:

Router::connect('/login', array('controller' => 'employees', 'action' => 'login'));
Router::connect('/login/*', array('controller' => 'employees', 'action' => 'login'));

I instead get the standard error when Cake can't find a controller for a page:

Error: LoginController could not be found.

Meaning it's not being routed at all; LoginController isn't supposed to be called nor does it exist. I've confirmed that the login function in EmployeesController is never even being started. The page should be routed to the EmployeesController login() function which generates the Employees Login view.

Did I miss a change in how Cake handles routing? Is there anything that might be the issue here?

Update:

I know order can matter with the routes, so here is the exact order of the related router statements in app/config/routes.php:

Router::connect('/pages/*', array('controller' => 'pages', 'action' => 'display'));

CakePlugin::routes();

require CAKE . 'Config' . DS . 'routes.php';

Router::connect('/', array('controller' => 'pages', 'action' => 'home'));

Router::connect('/login', array('controller' => 'employees', 'action' => 'login'));
Router::connect('/login/*', array('controller' => 'employees', 'action' => 'login'));
Router::connect('/logout', array('controller' => 'employees', 'action' => 'logout'));

There are several more routes but they're all just pretty URLs aren't aren't called in this situation.

In addition this is the function from EmployeesController that redirects the user to the actual /login URL. Everything runs fine until this redirect:

    function doLogin() {
    $goto = (isset($this->params['url']['url'])) ? $this->params['url']['url'] : '';
    $goto = $goto = str_replace('/','|',$goto);
    $goto = $goto = str_replace(':','~',$goto);
    if (strpos($goto,'|')===0) $goto = substr($goto,1);
    if ($goto) $goto = '/goto:'.$goto;
    $this->redirect('/login/'.$goto);
    die();
}

The $goto/str_replace() stuff is just to construct a URL to go to after the user has logged in successfully. It's not being used in this situation, we're just being redirected to /login/ Do I need to redirect a different way now? This redirect IS going to the right URL, but once there it is NOT being routed, despite the correct router:connect statement.

Update: The order of the routes in routes.php appears to be part of the issue; I changed my route for /login to be above the require CAKE . 'config' . DS . 'routes.php' as such:

Router::connect('/login', array('controller' => 'employees', 'action' => 'login'));
Router::connect('/login/*', array('controller' => 'employees', 'action' => 'login'));
require CAKE . 'Config' . DS . 'routes.php';

I've confirmed that now the 'login' action from EmployeesController.php now executes, so they routing is being done right to that extent. However I now get the following error:

Notice (8): Undefined index: datasource [CORE\Cake\Model\ConnectionManager.php, line 252]
Errors Missing Datasource Class
Error: Datasource class was not found.

Is this caused because I'm routing before Cake's default routes? I'm using Cake's default database plugins (Sqlserver) to connect, did this routing change break the routing to the database plugin? If it's a different issue I can create a separate question, because if the above fix is unrelated to the missing datasource, the original issue is fixed.


Solution

  • No, as i commented you before, your routing is ok. check my screencapture. I have Cakephp 2.0 Beta, just testing with your code to route to EmployeesController. Maybe another Code is corrupting the routing sequence, maybe Cakephp 2.0 have cache for routing.

    ScreenShot