cakephp 中的链接不相关

发布于 2024-08-31 21:20:21 字数 588 浏览 6 评论 0原文

在我的布局中,我有一个作为元素包含在内的菜单,并且它包含一个像这样的链接。

<? $html->link('New Part Number','/part_numbers/add'); ?>

我遇到的问题是 cake 没有正确重定向,它最终将我发送到“http://localhost/part_numbers /add”而不是“http://localhost/my_client_folder/client_app/part_numbers/add< /a>”(我正在本地构建)。我的 CakePHP 应用程序存储在 webroot 文件夹下的几个目录中,但我认为 CakePHP 会自动检测如何构建链接,无论应用程序位于何处,对吗?

那么我是否需要配置应用程序根文件夹,或者创建路由之类的?

In my layout, I have a menu that I've included as an element, and it contains a link like so.

<? $html->link('New Part Number','/part_numbers/add'); ?>

The problem that I have is that cake isn't redirecting correctly and it ends up sending me to "http://localhost/part_numbers/add" instead of "http://localhost/my_client_folder/client_app/part_numbers/add" (I'm building this locally). My CakePHP app is stored a few directories below my webroot folder, but I thought CakePHP would autodetect how to build the linking no matter where the application was located, right?

So do I need to configure the application root folder, or create a route or something?

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

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

发布评论

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

评论(4

清秋悲枫 2024-09-07 21:20:21

如果您使用示例中的 URL 而不是 array('controller', 'action'),您的解决方案没有什么不同。

解决方案是放置 到您的标题中。

这将使所有链接(也是 src 标签)相对于您的 webroot。它不会影响 JS/CSS URL-s!

Your solution is no different if you would use an URL like in your example instead of array('controller', 'action').

Solution is to put <base href="<?php echo 'http://'.$_SERVER['SERVER_NAME'].Router::url('/'); ?>" /> into your headers.

This will make all links (also src tags) relative to your webroot. It won't affect JS/CSS URL-s!

帝王念 2024-09-07 21:20:21

一般来说,作为一种纪律,我建议始终在视图和控制器操作中使用反向路由。根据我的经验,一旦您开始尝试使用任何高级路由功能,“智能 URL”(例如 /part_numbers/add)就会很快崩溃。

它还违反了一次编写代码,多次读取代码的原则——识别由 /part_numbers/add 这样的简单路由调用的控制器操作可能很简单,但在具有大量自定义的大型应用程序中路由,如果您一致地使用反向路由数组,那么弄清楚链接将调用什么操作就会变得更加简单。

In general, I would recommend always always always using reverse-routing in your views and controller actions, as a matter of discipline. In my experience, "smart URLs" (e.g. /part_numbers/add) break down very quickly once you start trying to use any of the advanced routing features.

It also violates the principle of writing code once, reading it many times – identifying the controller action invoked by a simple route like /part_numbers/add may be simple, but in a larger application with a ton of custom routes, it becomes much simpler to figure out what action your links will invoke if you use reverse-routing arrays consistently.

死开点丶别碍眼 2024-09-07 21:20:21

谢谢大家的解决方案,我之前的解决方案确实是错误的:

你必须从“$this”构建它,这样它才知道你来自哪里,否则它无法弄清楚如何构建相对链接。

Html->link("New Part Number", array('controller' => 'part_numbers', 'action' => 'add')); ?>

链接不起作用的真正原因(如下文所述)是因为未指定数组。这应该可以修复链接:

<?= $html->link("New Part Number", array('controller' => 'part_numbers', 'action' => 'add')); ?>

Thank you everyone for your solutions, my previous solution was indeed in error:

You have to build it off of "$this" so that it knows where you're coming from, otherwise it can't figure out how to build a relative link.

Html->link("New Part Number", array('controller' => 'part_numbers', 'action' => 'add')); ?>


The REAL reason that the links were not working, as kindly mentioned below, was because of not specifying the array. This should work to fix the link:

<?= $html->link("New Part Number", array('controller' => 'part_numbers', 'action' => 'add')); ?>
清泪尽 2024-09-07 21:20:21

您还可以使用:

Router::url(array(controller => '', action => '', true))

You can also use:

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