2009-10-29 119 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 
相关问题