CakePHP 未输出正确的 CSS 路径

发布于 2024-11-09 13:42:14 字数 370 浏览 0 评论 0原文

刚刚开始使用 CakePHP 进行开发,安装并配置它。但是,应用程序不会输出样式表的正确路径。

查看页面源代码时,样式表的路径为:

/rm-lab/css/cake.generic.css

这会导致 404 错误页面。

如果我尝试通过此路径访问样式表,则会加载样式表;

/rm-lab/app/css/cake.generic.css

感谢您的帮助。

更新: 在尝试了多种解决方案之后,我刚刚在另一个子文件夹中全新安装了 cakePHP,这可以很好地加载 CSS。我认为这与 .htaccess 文件有关。

Just started developing in CakePHP, installed it and configured it. However the application doesn't output the correct path to the stylesheet.

When viewing the source of the page the path to the stylesheet is:

/rm-lab/css/cake.generic.css

This leads to 404 error page.

The stylesheet loads if I try to access it through this path;

/rm-lab/app/css/cake.generic.css

Appreciate the help.

UPDATE:
After trying numerous solutions, I just did a fresh install of cakePHP in another subfolder and this loads the CSS fine. I think it was to do with the .htaccess files.

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

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

发布评论

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

评论(6

笙痞 2024-11-16 13:42:14

您的蛋糕应用程序似乎位于子文件夹中。您应该在 app/webroot 中的 .htaccess 文件中设置重写基础

在 app/webroot/.htaccess 中,添加以下行:

RewriteBase /rm-lab

您的 .htaccess 文件现在应如下所示:

<IfModule mod_rewrite.c>
  RewriteEngine On
  RewriteBase /rm-lab
  RewriteCond %{REQUEST_FILENAME} !-d
  RewriteCond %{REQUEST_FILENAME} !-f
  RewriteRule ^(.*)$ index.php?url=$1 [QSA,L]
</IfModule>

It appears that your cake app is in a subfolder. You should set the rewrite base in your .htaccess file in app/webroot

In app/webroot/.htaccess, add the following line:

RewriteBase /rm-lab

Your .htaccess file should now appear as such:

<IfModule mod_rewrite.c>
  RewriteEngine On
  RewriteBase /rm-lab
  RewriteCond %{REQUEST_FILENAME} !-d
  RewriteCond %{REQUEST_FILENAME} !-f
  RewriteRule ^(.*)$ index.php?url=$1 [QSA,L]
</IfModule>
寂寞陪衬 2024-11-16 13:42:14

您使用主题吗?如果是这样,您可能需要切换路径。请参阅此处(向下滚动一点):

http://book.cakephp.org/view/1093/主题

Are you using a theme? If so, you might need to switch the path. See here (scroll down a bit):

http://book.cakephp.org/view/1093/Themes

独夜无伴 2024-11-16 13:42:14

从我的角度来看,这似乎是一个 Apache 级别的问题。 Apache 中的 DocumentRoot 是否设置为 /app 目录?这肯定会有帮助。一般来说,我认为您应该使用 Cake 检查 Apache 的内容(查看应用程序中的 .htaccess 文件)。为了查明问题,我将 CakePHP 配置为在 core.php 中使用 mod_rewrite。

Off the top of my head, this seems like an Apache-level problem. Is your DocumentRoot in Apache set to the /app directory? This would definitely help. In general, I think you should examine the Apache stuff with Cake (look at your .htaccess files in the app). To pinpoint the problem, I would configure CakePHP to not use mod_rewrite in core.php.

马蹄踏│碎落叶 2024-11-16 13:42:14

尝试在 public_html 文件夹中创建一个名为“test_rewrite.php”的新脚本
访问该文件以确保您的共享主机帐户上启用了 mod_rewrite。如果未加载,您可能需要联系您的托管提供商以启用它。

<?php
if (in_array("mod_rewrite", apache_get_modules())) {
    echo "mod_rewrite loaded";
} else {
    echo "mod_rewrite not loaded";
}
?>

Try making a new script in your public_html folder called "test_rewrite.php"
Access the file to make sure that mod_rewrite is enabled on your shared hosting account. If it's not loaded, you may want to contact your hosting provider to enable it.

<?php
if (in_array("mod_rewrite", apache_get_modules())) {
    echo "mod_rewrite loaded";
} else {
    echo "mod_rewrite not loaded";
}
?>
灯角 2024-11-16 13:42:14

我认为您正在尝试使 HtmlHelper::css 方法执行不应执行的操作。根据文档:

(需要混合 $path)-- CSS 的名称
样式表或包含的数组
CSS 样式表的名称。如果 $path 是
以“/”为前缀,路径将是
相对于您的网络根目录
应用程序。否则,该路径将
通常与您的 CSS 路径相关
webroot/css

cake.generic.css 文件确实应该使用 $this->Html->css(' 放置在 app/webroot/css 文件夹中cake.generic') 您在视图中使用的代码。

以下是 API 文档的链接:HtmlHelper 类文档

I think you are trying to make the HtmlHelper::css method do something that it isn't supposed to do. According to the documentation:

(mixed $path required) -- The name of a CSS
style sheet or an array containing
names of CSS stylesheets. If $path is
prefixed with '/', the path will be
relative to the webroot of your
application
. Otherwise, the path will
be relative to your CSS path, usually
webroot/css
.

The cake.generic.css file should indeed be placed within the app/webroot/css folder using the $this->Html->css('cake.generic') code that you are using in your view.

Here's a link to the API documentation: HtmlHelper Class documentation

朦胧时间 2024-11-16 13:42:14

确保您的 apache DocumentRoot 设置为 /path/to/your/site/in/a/subdir/webroot

webroot 部分至关重要 - 它阻止访问 webroot 外部的文件,并且 webroot 文件夹中的 htaccess 会跳过加载为 webroot 内的实际文件(img、css 等)调度 index.php 文件

如果您正确设置了 DocumentRoot 路径,并且从站点文件夹的根目录到 cake 安装的子文件夹包含在 .htaccess 中如上所述,文件路径问题应该会自行解决。

您可能想让我们知道蛋糕安装在服务器上的绝对路径,并向我们展示包含 DocumentRoot 详细信息的 VirtualHost 块。

Make sure your apache DocumentRoot is set to /path/to/your/site/in/a/subdir/webroot

The webroot part is critical - it prevents files from outside the webroot from being accessible and the htaccess in the webroot folder skips loading the dispatch index.php file for actual files inside the webroot (img, css, etc)

If you have the DocumentRoot path set right and the subfolder from the root of the site folder to the cake install is included in the .htaccess file as mentioned above the pathing issue should fix itself.

You might want to let us know the absolute path on the server to the cake install as well as show us the VirtualHost block with the DocumentRoot details.

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