【发布时间】:2013-10-22 18:34:58
【问题描述】:
我正在使用 CakePHP 2.1。这是交易...
我想要一个这种格式的网址:
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]
那么,现在的问题是:为什么它在路径的第一级(domain.com/:nickname)中不起作用?
【问题讨论】:
-
您能否发布您的完整路线文件?
-
我不确定你需要在那里有
:username,因为它不是:controller或:action的参数可能需要查看手册book.cakephp.org/2.0/en/development/… -
@DavidYell,我试图对路由元素做一些事情:book.cakephp.org/2.0/en/development/routing.html#route-elements 但我不确定是否可以在不指定任何控制器或操作的域之后立即拥有这个“元素”。 ..阅读文档可以做到这一点,但不起作用...
-
@Line 您是否尝试过使用命名参数对? /username:davidyell 然后
$this->request->params['named']['username'] -
我尝试过这样的事情:domain.com/users/:nickname 并且以这种方式工作......然后我得到了:$this->request->params['pass '][0] (我必须在路由器上声明我正在传递这个转发)。所以,现在的问题是:为什么它在路径的第一级(domain.com/:nickname)中不起作用? :(