开发者

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('contr开发者_如何转开发oller' => '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.


I'm upgrading a CakePHP app to 2.1.1 and have got the same problem. The way you configure the database.php setup has changed. You need to point to the correct datasource like so....

public $default = array(
    'datasource' => 'Database/Mysql',
    'persistent' => false,
    'host' => 'localhost',
    'port' => '',
    'login' => 'cakeBlog',
    'password' => 'c4k3-rUl3Z',
    'database' => 'cake_blog_tutorial',
    'schema' => '', 
    'prefix' => '',
    'encoding' => ''
);

The 'driver' has changed from 'driver' to 'datasource'. I hope that helps


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.

Cake PHP Routing issue in 2.0Beta


Finally got a connection with this sequence for sqlserver:

    public $default = array(
    'datasource' => 'Database/Sqlserver',
    'persistent' => true,
    'host' => 'VPR132989\LOCAL',
    //'login' => 'markvTest',
    //'password' => 'Pa55w0rd',
    'database' => 'blogTutorial',
    'schema' => '',
    'flags' => array(),
    //'encoding' => PDO::SQLSRV_ENCODING_UTF8,
);

I commented out the login and password to test windows authentication. Be sure to restart services. The encoding depends if you have this variable defined on your driver and it seems to be driver specific. Hope this helps

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜