CakePHP HTML 帮助器 CSS 标签和 Apache Alias
作为 CakePHP 的相对新手,我希望获得一些关于配置 Apache 和 CakePHP 来查找包含文件(CSS、JavaScript 等)的“正确”方法的建议。
如果我的服务器的 DocumentRoot
设置为 /var/www
并且我在 /var/www/somepath/cakeapp
中安装和配置 CakePHP,我可以访问应用程序按预期在 URL http://example.com/somepath/cakeapp 处运行。
但是,如果我使用 HTML 帮助程序在默认布局中生成 CSS 链接标记,我就会开始遇到麻烦。例如,代码
echo $html->css('styles');
生成以下标签:
<link rel="stylesheet" type="text/css" href="/somepath/cakeapp/css/styles.css" />
然而,CSS 实际上位于 /somepath/cakeapp/app/webroot/css
中。
如果 CakePHP 应用程序是我的域中唯一的东西,我可以按照文档的建议将 DocumentRoot
指向 /var/www/somepath/cakeapp/app/webroot
,并且所有会(大概)很好。然而,这对我来说不是一个选择。
是否有普遍接受的正确方法来配置 Apache 和 CakePHP,以便 HTML 帮助程序可以生成正确的链接标记?
编辑:我觉得一定有一些我还没有遇到过的 CakePHP 配置和 Apache Aliases 的组合。
As a relative newcomer to CakePHP, I'm hoping for some advice on the "right" way of configuring Apache and CakePHP to find included files (CSS, JavaScript, etc).
If my server's DocumentRoot
is set to /var/www
and I install and configure CakePHP in /var/www/somepath/cakeapp
I can access the application as expected at the URL http://example.com/somepath/cakeapp.
However, if I use the HTML helper to generate the CSS link tag in my default layout, I start to run into trouble. For example, the code
echo $html->css('styles');
produces this tag:
<link rel="stylesheet" type="text/css" href="/somepath/cakeapp/css/styles.css" />
However, the CSS actually lives in /somepath/cakeapp/app/webroot/css
.
If the CakePHP app were the only thing on my domain, I could point the DocumentRoot
at /var/www/somepath/cakeapp/app/webroot
as the documentation suggests and all would (presumably) be well. However, that's not an option for me.
Is there a generally accepted correct way of configuring Apache and CakePHP so that the HTML helper can produce the correct link tag?
Edit: I feel there must be some combination of CakePHP configuration and Apache Aliases that I haven't run across yet.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
/somepath/cakeapp/css/styles.css
实际上是需要在链接中显示的正确路径。之所以如此显示,是因为 cakeapp 目录中的 .htaccess 文件将请求重定向(重写)到 cakeapp/app/webroot 目录。
现在,这种结构存在的原因是因为通常您的应用程序中有“公共”和“非公共”部分。 “非公开”部分包含 PHP 代码,包括控制器、模型、视图、插件、蛋糕库等。“公开”部分包括 css、脚本和图像文件。
现在,您可以将 /var/www/html 文件夹作为您的应用程序目录以确保其安全。为了根据您的喜好完全配置您的应用程序,请检查 cakeapp/app/bootstrap.php 文件并更改相关设置。您可以在此处了解更多信息。
简而言之,不用担心。你看到的路是对的!
/somepath/cakeapp/css/styles.css
is actually the correct path that needs to be displayed in the links.The reason why it gets displayed as such is because of the .htaccess file in your cakeapp directory that redirects (rewrites) requests to the cakeapp/app/webroot directory.
Now, the reason why this structure even exists is because generally, you have "public" and "non-public" parts in your application. The "non-public" parts contain the PHP code including controllers, models, views, plugins, cake library, etc. The "public" parts include the css, scripts, and image files.
Now, you could make your /var/www/html folder as your app directory to make it secure. In order to completely configure your application to your liking, check the cakeapp/app/bootstrap.php file and change the relevant settings. You can learn more about that here.
In short, no worries. The path you see is right!
尝试在 Cake 根目录 /app 和 /app/webroot 中的 .htaccess 文件中定义 RewriteBase 以指向您的子目录。将它们设置为各自的路径 /somepath/、/somepath/app/ 和 /somepath/app/webroot/
Try defining RewriteBase in your .htaccess files in Cake's root directory, /app and /app/webroot to point to your subdirectory. Set them to the respective paths, /somepath/, /somepath/app/ and /somepath/app/webroot/