0

CakePHP 2.1 を使用しています。これが取引です...

この形式の 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]

では、問題は次のとおりです。パスの最初のレベル (domain.com/:nickname) で機能しないのはなぜですか?

4

1 に答える 1

0

試す:

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

そして、あなたが持っている場合は、UsersController で

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

$username には URL からのユーザー名が含まれます

于 2012-06-03T23:59:55.543 に答える