【发布时间】:2009-10-31 05:26:39
【问题描述】:
我正在尝试在 CakePHP 中制作一个简单的重定向控制器。我希望 URL 的格式为:
http://example.com/redirector/<numeric id>/<url to redirect to>
例如,
http://example.com/redirector/1/http://www.google.com
当然,我需要重定向的 URL 可能更复杂,包括斜线、参数和锚点。
我似乎无法弄清楚如何编写路由配置,以便我的操作看起来像:
class RedirectsController extends AppController {
function myredirectaction($id, $url) {
$this->autoRender = false;
$this->redirect($url);
}
似乎无论我尝试什么,url-to-redirect-to 中的“/”都会混淆我的路由尝试并将 URL 分成几部分,这不再符合我的操作定义。我该怎么办?
我是 PHP 和 CakePHP 的新手,因此您可以提供任何建议。
更新:
因此,它不是上面的示例 URL,而是经过 URL 转义,如下所示:
http://example.com/redirector/1/http%3A%2F%2Fwww.google.com
但是,我的路由仍然无法正常工作。这是我在 routes.php 中的内容:
Router::connect(
'/redirector/:id/:url',
array('controller' => 'redirects', 'action' => 'myredirectaction'),
array(
'id' => '[0-9]+',
'url' => 'http.*'
)
);
这是我尝试该 URL 时得到的结果:
Warning (2): array_merge() [function.array-merge]: Argument #1 is not an array [CORE/cake/dispatcher.php, line 301]
Code | Context
$fromUrl = "redirector/1/http://www.google.com"
$params = array(
"pass" => array(),
"named" => array(),
"id" => "1",
"url" => "http://www.google.com",
"plugin" => null,
"controller" => "redirects",
"action" => "myredirectaction",
"form" => array()
)
$namedExpressions = array(
"Action" => "index|show|add|create|edit|update|remove|del|delete|view|item",
"Year" => "[12][0-9]{3}",
"Month" => "0[1-9]|1[012]",
"Day" => "0[1-9]|[12][0-9]|3[01]",
"ID" => "[0-9]+",
"UUID" => "[A-Fa-f0-9]{8}-[A-Fa-f0-9]{4}-[A-Fa-f0-9]{4}-[A-Fa-f0-9]{4}-[A-Fa-f0-9]{12}"
)
$Action = "index|show|add|create|edit|update|remove|del|delete|view|item"
$Year = "[12][0-9]{3}"
$Month = "0[1-9]|1[012]"
$Day = "0[1-9]|[12][0-9]|3[01]"
$ID = "[0-9]+"
$UUID = "[A-Fa-f0-9]{8}-[A-Fa-f0-9]{4}-[A-Fa-f0-9]{4}-[A-Fa-f0-9]{4}-[A-Fa-f0-9]{12}"
$url = array(
"url" => "/redirector/1/http://www.google.com"
)
array_merge - [internal], line ??
Dispatcher::parseParams() - CORE/cake/dispatcher.php, line 301
Dispatcher::dispatch() - CORE/cake/dispatcher.php, line 118
[main] - APP/webroot/index.php, line 88
由于我的操作没有得到两个预期的参数,因此发出了更多警告。
当然,在使用 $url 之前,我已经将操作更改为 urldecode($url)。
【问题讨论】: