2009-10-29 47 views
1

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

回答

2

要放置斜線和其他特殊字符,請改爲使用ACSII代碼。對於代碼和各自的字符的列表,請參閱本文檔:

http://www.ascii.cl/htmlcodes.htm

+0

事實證明,即使是編碼URL,服務器仍然將解碼的URL傳遞給PHP。這可能可以通過更改服務器配置來更改,但這不是所有情況下的選項。 – 2009-10-30 20:02:11

1

您需要添加一通數組變量傳遞到你的行動。

Router::connect(
     '/redirector/:id/:url', 
     array('controller' => 'redirects', 'action' => 'myredirectaction'), 
     array(
       'id' => '[0-9]+', 
       'url' => 'http.*', 
       'pass' => array('id', 'url') 
     ) 
); 

您可以找到有關爲什麼在這裏的附加信息:我好像http://book.cakephp.org/view/543/Passing-parameters-to-action

+0

這確實有幫助,但仍然不足以根據編碼/解碼問題和路徑分隔符處理URL。 – 2009-10-30 20:03:20

+0

由於URL中的冒號和可能的問號,該URL需要進行URL編碼。否則,你可以調用func_get_args()並用斜線重新加入參數。 我不確定任何其他http服務器,但是如果您使用的是Apache,那麼您也可以檢查是否設置了AllowEncodedSlashes。如果編碼的URL不在,它可能會導致編碼的URL出現問題。 – Jason 2009-10-30 23:02:13

0

不能使這項工作與URL像我本來希望。我能做的最好的事情就是提供重定向到參數中的URL。所以路由變得簡單:

Router::connect(
     '/redirector', 
     array('controller' => 'redirects', 'action' => 'myredirectaction') 
); 

而控制器動作變得(處理省略錯誤):

function myredirectaction() { 
    $this->autoRender = false; 
    $redirect_url = $this->params['url']['theurl']; 
    $this->redirect($redirect_url); 
} 

和URL的形式爲:

http://example.com/redirector?theid=1&theurl=http://www.google.com 
相關問題