自定义路由元素不能在cakephp中工作
问题描述:
我正在使用CakePHP 2.1。事情是这样的......自定义路由元素不能在cakephp中工作
我想在这个格式的URL:
http://mysite.com/[username]/
哪里[username]
可以是动态的,并呼吁已经实施的“用户”控制器。
下面是routes.php文件中定义的路径:
Router::connect(
'/:username',
array('controller' => 'users', 'action' => 'profile'),
array(
'pass' => array('username'),
'username' => '[a-zA-Z0-9][/-_.]+'
));
如果我试图达到http://mysite.com/testuser显示此错误:
"Missing Controller
Error: testuserController could not be found."
这里是我的整个routes.php文件文件:
<?php
/**
* Routes configuration
*
* In this file, you set up routes to your controllers and their actions.
* Routes are very important mechanism that allows you to freely connect
* different urls to chosen controllers and their actions (functions).
*
* PHP 5
*
* CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
* Copyright 2005-2012, Cake Software Foundation, Inc. (http://cakefoundation.org)
*
* Licensed under The MIT License
* Redistributions of files must retain the above copyright notice.
*
* @copyright Copyright 2005-2012, Cake Software Foundation, Inc. (http://cakefoundation.org)
* @link http://cakephp.org CakePHP(tm) Project
* @package app.Config
* @since CakePHP(tm) v 0.2.9
* @license MIT License (http://www.opensource.org/licenses/mit-license.php)
*/
Router::connect('/', array('controller' => 'home', 'action' => 'index'));
Router::connect(
'/:username',
array('controller' => 'users', 'action' => 'profile'),
array(
'pass' => array('username'),
'username' => '[a-zA-Z0-9][/-_.]+'
));
/**
* Load all plugin routes. See the CakePlugin documentation on
* how to customize the loading of plugin routes.
*/
CakePlugin::routes();
/**
* Load the CakePHP default routes. Remove this if you do not want to use
* the built-in default routes.
*/
require CAKE . 'Config' . DS . 'routes.php';
我试过这样的事情:
Router::connect(
'/users/:username',
array('controller' => 'users', 'action' => 'profile'),
array(
'pass' => array('username'),
'username' => '[a-zA-Z0-9][/-_.]+'
));
和这种方式它工作...!然后我可以得到︰$ this-> request-> params ['pass'] [0]
所以,现在这是一个问题:为什么它不能在路径的第一级(域。 COM /:昵称)?
答
尝试:
Router::connect('/*', array('controller' => 'users', 'action' => 'actionName'));
,然后在UsersController如果你有
public function foo($username){
...
}
$用户名包含在URL
的用户名能否请您发表您的完整路径文件? – Joep 2012-04-12 14:19:15
@Joep它在那里:) – Line 2012-04-12 14:30:13
我不确定你需要在那里有':username',因为它的参数不是':controller'或':action'可能需要检查手册,http:// book.cakephp.org/2.0/en/development/routing.html#routes-configuration – 2012-04-12 16:32:17