我想在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