简单URL重定向器的路由配置和控制器操作参数

时间:2009-10-29 18:55:40

标签: php cakephp

我正在尝试在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中的“/”都会混淆我的路由尝试并将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)。

3 个答案:

答案 0 :(得分:2)

要放置斜杠和其他特殊字符,请改用ASCII码。有关代码及其各自字符的列表,请参阅此文档:

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

答案 1 :(得分: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

答案 2 :(得分: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);
}

网址格式为:

http://example.com/redirector?theid=1&theurl=http://www.google.com