【问题标题】:CakePHP 2.0 Router::connect how to pass named paramsCakePHP 2.0 Router::connect 如何传递命名参数
【发布时间】:2012-02-11 02:46:56
【问题描述】:

我正在使用 CakePHP 2.0 重建一个站点,需要将一些旧 URL 路由到新 URL。例如,这个:

http://www.example.com/widget/helpbox/location/mackay-qld

将路由到这个:

http://www.example.com/widgets/answer/location_id:10542

为了做到这一点,我有以下路线:

Router::connect(
    '/widget/helpbox/location/mackay-qld',
    array(
        'controller' => 'widgets',
        'action' => 'answer',
        'location_id' => 10542
    )
);

当我调试 $this->request->params 时,我得到了这个:

Array
(
    [plugin] => 
    [controller] => widgets
    [action] => answer
    [named] => Array
        (
        )

    [pass] => Array
        (
        )

    [location_id] => 10542
    [isAjax] => 
)

但我希望这样:

Array
(
    [plugin] => 
    [controller] => widgets
    [action] => answer
    [named] => Array
        (
            [location_id] => 10542
        )

    [pass] => Array
        (
        )

    [isAjax] => 
)

我也试过打电话

Router::connectNamed(array('location_id'));

...但无济于事。 location_id 仍然以相同的方式传递 - 不是作为命名参数。

有人知道正确的语法吗?

【问题讨论】:

标签: cakephp cakephp-2.0


【解决方案1】:

在提交票证并得到票证无效的响应后,这迫使我深入挖掘。我终于明白需要做什么了。由于入站 URL 不包含命名参数,因此无法按照您尝试的方式建立路由连接。路线连接用作如何将某些位置路由到新位置的模板。但是,您要做的是将特定 URL 路由到新 URL。您正在寻找的是重定向。

Router::redirect(
    '/widget/helpbox/location/mackay-qld',
    array(
        'controller' => 'widgets',
        'action' => 'answer',
        'location_id' => 10542,
    ),
);

这应该会返回您正在寻找的结果。

【讨论】:

  • 很高兴听到! Mark Story 通过将我的错误设置为无效,让我走上了正确的道路。哈哈。但我很高兴它对你有用。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-04-14
  • 1970-01-01
  • 1970-01-01
  • 2013-02-05
  • 2011-05-12
相关资源
最近更新 更多