2012-04-12 42 views
0

我正在使用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 /:暱稱)?

+0

的用戶名能否請您發表您的完整路徑文件? – Joep 2012-04-12 14:19:15

+0

@Joep它在那裏:) – Line 2012-04-12 14:30:13

+0

我不確定你需要在那裏有':username',因爲它的參數不是':controller'或':action'可能需要檢查手冊,http:// book.cakephp.org/2.0/en/development/routing.html#routes-configuration – 2012-04-12 16:32:17

回答

0

嘗試:

Router::connect('/*', array('controller' => 'users', 'action' => 'actionName')); 

,然後在UsersController如果你有

public function foo($username){ 
    ... 
} 

$用戶名包含在URL