且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

cakephp 2 CSS,JavaScript和链接在本地计算机/本地主机上不起作用

更新时间:2022-12-23 22:46:37

您的问题是在localhost上,您的网站在Apache文档根目录的子文件夹中运行,而您的链接中没有指定此子文件夹。因此,如果文件位于 C:/xampp/htdocs/www/app/webroot/css/mysheet.css 中,则正确的访问方式为 < link rel = stylesheet href = / www / css / mysheet.css>

The core of your problem is that on localhost you are running your website in subfolder in Apache document root, and you do not have this subfolder specified in your links. So if file resides in C:/xampp/htdocs/www/app/webroot/css/mysheet.css, correct way to reach it would be <link rel="stylesheet" href="/www/css/mysheet.css">.

您在这里有两个选择,您可以(而且我认为应该)同时使用它们:

You have two options here, and you can (and I think you should) use BOTH of them:


  1. 使用HtmlHelper

在您的视图中,通过调用 $ this-> Html-> css( mysheet)链接到您的工作表,它还将包括其基本文件夹。更多信息: HtmlHelper

By calling $this->Html->css("mysheet") in your view, Cake will create link to your sheet, and it will include also its base folder. More info: HtmlHelper


  1. 使用本地域

您可以设置在xampp配置中设置本地虚拟主机。之后,您将可以使用此域而不是localhost / www访问本地网站。在下面的示例中,我将使用域mywebsite.local:

You can set up local virtual host in your xampp config. After doing so, you will be able to reach your local website using this domain, instead of localhost/www. In example below I will use domain mywebsite.local:

2.1。转到C:/ xampp / apache / conf / extra并打开httpd-vhosts.conf。

2.1. Go to C:/xampp/apache/conf/extra and open httpd-vhosts.conf.

2.2。在文件末尾添加以下代码:

2.2. Add following code to end of file:

< VirtualHost *:80>
DocumentRoot C:/ xampp / htdocs / www
ServerName mywebsite.local
< / VirtualHost>

2.3。转到C:/ Windows / System32 / drivers / etc并以管理员权限打开hosts文件。

2.3. Go to C:/Windows/System32/drivers/etc and open hosts file with admin permissions.

2.4。添加以下行: 127.0.0.1 mywebsite.local

2.4. Add following line: 127.0.0.1 mywebsite.local

2.5。重新启动Apache。

2.5. Restart Apache.

现在,您可以通过在浏览器地址栏中输入 http://mywebsite.local 。由于此虚拟主机的文档根目录现在为htdocs / www,因此所有CSS都应加载。

Now, you can access your local website by typing in your browser address bar http://mywebsite.local. As document root for this virtual host is now htdocs/www, all css should load.