使用 Cakephp url 的正确方法是什么?

发布于 2024-09-05 06:12:24 字数 1263 浏览 6 评论 0原文

这是我在这里发表的第一篇文章:)

我在处理 url 和参数时遇到了一些困难。我一遍又一遍地浏览路由器类 api 文档,但没有发现任何有用的东西。

首先,我想知道 CakePHP(1.3) 中是否有任何“通用”格式用于处理 url。我目前将所有 url 作为简单数组处理(采用 Router::url 和 $html->link 接受的格式),只要我只需要将它们作为参数传递给 cake 自己的方法,就很容易。如果我需要其他东西,通常会变得很棘手。

主要是我在将字符串 url 转换为基本数组格式时遇到问题。 假设我想将 $arrayUrl 转换为字符串,然后再次转换为 url:

$arrayUrl=array('controller'=>'SomeController','action'=>'someAction','someValue');
$url=Router::url($arrayUrl);       //$url is now '/path/to/site/someController/someAction/someValue'
$url=Router::normalize($url);      //remove '/path/to/site'
$parsed=Router::parse($url);       /*$parsed is now
Array(
    [controller] => someController
    [action] => someAction
    [named] => Array()
    [pass] => Array([0] => someValue)
    [plugin] => 
) */

这似乎需要大量代码来完成像在两种核心格式之间进行转换这样简单的事情。另外,请注意 $parsed 仍然与 $arrayUrl 不同。当然,我可以手动调整 $parsed ,实际上我已经这样做了几次作为快速补丁,但我想深入了解这一点。

我还注意到,当使用前缀路由时,控制器中的 $this->params 将前缀嵌入到操作中(即 [action] => 'admin_edit'),而 Router::parse() 的结果则没有。当然,两者在其自己的密钥中都有前缀。

总而言之,如何以正确的方式在上述 3 种(或 4 种,如果包含前缀)格式之间转换 url? 当然,破解这个方法很容易,但我仍然愿意相信蛋糕是由一群比我有更多经验和洞察力的人开发的,所以我猜有一个很好的方法造成这种“不当行为”的原因。

我试图尽可能好地提出我的问题,但由于我生疏的英语技能,我不得不走一些弯路:)如果需要的话我会解释更多。

it's my first post here :)

I'm having some difficulties with dealing with urls and parameters. I've gone through the router class api documentation over and over again and found nothing useful.

First of all, I'd like to know if there is any 'universal' format in CakePHP(1.3) for handling urls. I'm currently handling all my urls as simple arrays(in the format that Router::url and $html->link accepts) and it's easy as long as I only need to pass them as arguments to cake's own methods. It usually gets tricky if I need something else.

Mainly I'm having problems with converting string urls to the basic array format.
Let's say I want to convert $arrayUrl to string and than again into url:

$arrayUrl=array('controller'=>'SomeController','action'=>'someAction','someValue');
$url=Router::url($arrayUrl);       //$url is now '/path/to/site/someController/someAction/someValue'
$url=Router::normalize($url);      //remove '/path/to/site'
$parsed=Router::parse($url);       /*$parsed is now
Array(
    [controller] => someController
    [action] => someAction
    [named] => Array()
    [pass] => Array([0] => someValue)
    [plugin] => 
) */

That seems an awful lot of code to do something as simple as to convert between 2 core formats. Also, note that $parsed is still not in the same as $arrayUrl. Of course I could tweak $parsed manually and actually I've done that a few times as a quick patch but I'd like to get to the bottom of this.

I also noticed that when using prefix routing, $this->params in controller has the prefix embedded in the action(i.e. [action] => 'admin_edit') and the result of Router::parse() does not. Both of course have the prefix in it's own key.

To summarize, how do I convert an url between any of these 3(or 4, if you include the prefix thing) mentioned formats the right way?
Of course it would be easy to hack my way through this, but I'd still like to believe that cake is being developed by a bunch of people who have a lot more experience and insight than me, so I'm guessing there's a good reason for this "perceived misbehavior".

I've tried to present my problem as good as I can, but due to my rusty english skills, I had to take a few detours :) I'll explain more if needed.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

┼── 2024-09-12 06:12:24

Cake URL 的“官方”格式应该是数组表示法 array('controller' => 'foo', 'action' => 'bar', 'baz', 'admin' => true)。每当您编写 URL 时,都应该使用这种格式。 Router 类会将这些转换为字符串 (/admin/foo/bar/baz) 或调度程序所需的信息 (array('named' => array(), 'pass ' => array(), ...)),具体取决于 URL 的使用位置。

您应该根据要调用的控制器操作来考虑它。 URL(作为字符串)只是在 Web 环境中实现此目的所必需的。您不需要使用 Dispatcher 格式。指定 URL 时也不应该使用字符串表示法,因为如果您想更改 URL 方案,则无法反向路由这些 URL。

也许您可以用一个例子来解释为什么需要将这三种形式从一种转换为另一种?

The "official" format for Cake URLs should be the array notation array('controller' => 'foo', 'action' => 'bar', 'baz', 'admin' => true). Whenever you write URLs, you should use this format. The Router class will translate those to either strings (/admin/foo/bar/baz) or information needed for the Dispatcher (array('named' => array(), 'pass' => array(), …)), depending on where the URL is used.

You should think of it in terms of which controller action you want to invoke. URLs (as strings) are only a necessary evil to accomplish this in a web context. There shouldn't be any need for you to use the Dispatcher format. You should also not use the string notation when specifying URLs, since these can't be reverse-routed if you ever want to change your URL scheme.

Maybe you could explain with an example why you need to convert these three forms from one to the other?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文