我想在CakePHP中创建一个简单的重定向器控制器。我想的URL的形式为:简单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中混淆了我的路由尝试,并将URL分割成多个部分,这不再符合我的动作定义。我该怎么办?
我是PHP和CakePHP的新手,所以你可以给任何建议表示赞赏。
更新:
因此而不是上面的示例网址,就已经逸出网址看起来像这样:
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.*'
)
);
这是我所得到的,当我尝试网址:从我的行动
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)。
事实证明,即使是编码URL,服务器仍然将解码的URL传递给PHP。这可能可以通过更改服务器配置来更改,但这不是所有情况下的选项。 – 2009-10-30 20:02:11