【问题标题】:Routing configuration and controller action parameters for simple URL redirector简单 URL 重定向器的路由配置和控制器操作参数
【发布时间】:2009-10-31 05:26:39
【问题描述】:

我正在尝试在 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-to 中的“/”都会混淆我的路由尝试并将 URL 分成几部分,这不再符合我的操作定义。我该怎么办?

我是 PHP 和 CakePHP 的新手,因此您可以提供任何建议。

更新:

因此,它不是上面的示例 URL,而是经过 URL 转义,如下所示:

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.*'
    )
);

这是我尝试该 URL 时得到的结果:

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)。

【问题讨论】:

    标签: php cakephp


    【解决方案1】:

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

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

    【讨论】:

    • 事实证明,即使对 URL 进行了编码,服务器仍将解码后的 URL 传递给 PHP。这也许可以通过更改服务器配置来改变,但这并不是所有情况下的选项。
    【解决方案2】:

    您需要添加一个传递数组以将变量传递给您的操作。

    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

    【讨论】:

    • 这确实有帮助,但由于编码/解码问题和路径分隔符,仍然不足以按照我想要的方式处理 URL。
    • 因为冒号和url中可能有问号,所以需要对URL进行URL编码。否则,您可以调用 func_get_args() 并自己用斜杠重新加入参数。我不确定是否有任何其他 http 服务器,但如果您使用的是 Apache,您还可以检查是否设置了 AllowEncodedSlashes。如果未打开,可能会导致编码 URL 出现问题。
    【解决方案3】:

    我似乎无法像我最初希望的那样使用 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
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-08-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-01-22
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多