CodeIgniter- CSS 当网站在本地主机上 运行 时未加载 JS
CodeIgniter- CSS & JS are not loading when the site is run on localhost
我是 CodeIgniter 和 MVC 架构的新手,但我非常了解 PHP。这是我面临的问题:
我通过cpanel下载了live网站的源代码(该网站是在codeignitor上开发的),现在我正在尝试在Localhost上修改网站。
我需要更改页面的样式,但我无法让网站在本地主机环境中加载它的 CSS 和 JS,只有 html 正在加载。
我检查了各种配置和环境设置。
在配置文件中,我为 localhost 更改了 $config['base_url']= "http://localhost/sitename/"
并且已经设置了其他配置变量(因为我下载了源代码)。
现在,当我 运行 在本地主机上使用 url http://localhost/sitename/
的站点时,只有 html 被加载而没有任何样式,css/js 什么都没有!
我还注意到以下内容:(认为它可能有用)
在 header.php
和 footer.php
等文件中,CSS 和 JS 文件包含在 <link href = "<?php echo WEB_ROOT; ?>/css/main.css" rel = "stylesheet" type = "text/css"/>
和 <script src="<?php echo WEB_ROOT; ?>/js/main.js"></script>
.
中
而实际上 css 和 js 文件实际上存在于 root\assets\css\
和 root\assets\js\
所以,
- 我应该如何在本地主机上查看完整的站点(因为它是在线的,包含所有 css 和 js)?我需要更改任何设置或脚本吗?
- 它在实时网站上的实际运行情况如何? (为访问 CSS & JS 设置了错误的路径)
求助。谢谢! (如果我需要提供更多详细信息,请告诉我)
确保你的 css 和图像等在应用程序文件夹之外
project > application
project > assets >
project > assets > css
project > assets > images
project > assets > js
然后自动加载 url 助手。 $this->load->helper('url');
然后
<link rel="stylesheet" type="text/css" href="<?php echo base_url('assets/css/styleshee.css');?>">
正如你所做的那样设置基数url
$config['base_url']= "http://localhost/sitename/";
如果您不使用 .htaccess
创建漂亮的 URL,您可能需要包含 index.php。
<link rel="stylesheet" type="text/css" href="<?php echo base_url('index.php/assets/css/styleshee.css');?>">
常量 WEB_ROOT
不是 Codeigniter 框架的一部分。所以要做的第一件事就是找到定义并更改它,使其与 localhost 域匹配。它可能被定义为特定的实时域。显然 localhost 是其他域。
或者,按照@wolfgang1983 在他的回答中所说的进行操作。 (这是我在写这篇文章时发布的,也是我要说的。)
我的做法是这样的:
<link rel="stylesheet" href=<?php echo base_url('assets/css/main.css'); ?> />
然后,当您的资产未加载时,您可以在网络浏览器中对其进行故障排除。您可以通过单击检查 > 网络来执行此操作。然后刷新您的页面并查看 codeigniter 尝试从何处读取文件。如果它不正确,您通常可以看到出了什么问题。 (例如,我曾经有过我的资产是从以下位置加载的:localhost/localhost/assets/*/*
希望对你有所帮助。
第一件事:
确保 apache 已打开 rewrite_mode
并且 .htaccess
文件位于正确的位置。
在 config.php 文件中:
$config['base_url'] = 'http://localhost/yourappfoldername/';
在控制器中:
public function __construct()
{
parent::__construct();
$this->load->helper(array('url'));
//======== or simply ===============
$this->load->helper('url');
}
你的 css 和 js url:
<link rel="stylesheet" type="text/css" href="<?php echo base_url('index.php/assets/css/styleshee.css');?>">
如果您使用 mod_rewrite 删除 index.php 页面,请将此变量设置为空白:
$config['index_page'] = '';
.htaccess 文件(删除 index.php):
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/ [L]
</IfModule>
根据 index.php
页面更改路线。如果删除 index.php:
<link rel="stylesheet" type="text/css" href="<?php echo base_url('/assets/css/styleshee.css');?>">
创建新文件夹并以您自己的名字命名。我创建了新文件夹调用 "assets"。然后将其放置在项目文件夹中,如下图所示。 image of the folder structure
然后我更改了 config.php 文件基础 url 路径。 image of the changed config.php file
之后我在控制器中添加了新的助手 class。 Image of the changed controller class
我是 CodeIgniter 和 MVC 架构的新手,但我非常了解 PHP。这是我面临的问题:
我通过cpanel下载了live网站的源代码(该网站是在codeignitor上开发的),现在我正在尝试在Localhost上修改网站。
我需要更改页面的样式,但我无法让网站在本地主机环境中加载它的 CSS 和 JS,只有 html 正在加载。
我检查了各种配置和环境设置。
在配置文件中,我为 localhost 更改了 $config['base_url']= "http://localhost/sitename/"
并且已经设置了其他配置变量(因为我下载了源代码)。
现在,当我 运行 在本地主机上使用 url http://localhost/sitename/
的站点时,只有 html 被加载而没有任何样式,css/js 什么都没有!
我还注意到以下内容:(认为它可能有用)
在 header.php
和 footer.php
等文件中,CSS 和 JS 文件包含在 <link href = "<?php echo WEB_ROOT; ?>/css/main.css" rel = "stylesheet" type = "text/css"/>
和 <script src="<?php echo WEB_ROOT; ?>/js/main.js"></script>
.
而实际上 css 和 js 文件实际上存在于 root\assets\css\
和 root\assets\js\
所以,
- 我应该如何在本地主机上查看完整的站点(因为它是在线的,包含所有 css 和 js)?我需要更改任何设置或脚本吗?
- 它在实时网站上的实际运行情况如何? (为访问 CSS & JS 设置了错误的路径)
求助。谢谢! (如果我需要提供更多详细信息,请告诉我)
确保你的 css 和图像等在应用程序文件夹之外
project > application
project > assets >
project > assets > css
project > assets > images
project > assets > js
然后自动加载 url 助手。 $this->load->helper('url');
然后
<link rel="stylesheet" type="text/css" href="<?php echo base_url('assets/css/styleshee.css');?>">
正如你所做的那样设置基数url
$config['base_url']= "http://localhost/sitename/";
如果您不使用 .htaccess
创建漂亮的 URL,您可能需要包含 index.php。
<link rel="stylesheet" type="text/css" href="<?php echo base_url('index.php/assets/css/styleshee.css');?>">
常量 WEB_ROOT
不是 Codeigniter 框架的一部分。所以要做的第一件事就是找到定义并更改它,使其与 localhost 域匹配。它可能被定义为特定的实时域。显然 localhost 是其他域。
或者,按照@wolfgang1983 在他的回答中所说的进行操作。 (这是我在写这篇文章时发布的,也是我要说的。)
我的做法是这样的:
<link rel="stylesheet" href=<?php echo base_url('assets/css/main.css'); ?> />
然后,当您的资产未加载时,您可以在网络浏览器中对其进行故障排除。您可以通过单击检查 > 网络来执行此操作。然后刷新您的页面并查看 codeigniter 尝试从何处读取文件。如果它不正确,您通常可以看到出了什么问题。 (例如,我曾经有过我的资产是从以下位置加载的:localhost/localhost/assets/*/*
希望对你有所帮助。
第一件事:
确保 apache 已打开 rewrite_mode
并且 .htaccess
文件位于正确的位置。
在 config.php 文件中:
$config['base_url'] = 'http://localhost/yourappfoldername/';
在控制器中:
public function __construct()
{
parent::__construct();
$this->load->helper(array('url'));
//======== or simply ===============
$this->load->helper('url');
}
你的 css 和 js url:
<link rel="stylesheet" type="text/css" href="<?php echo base_url('index.php/assets/css/styleshee.css');?>">
如果您使用 mod_rewrite 删除 index.php 页面,请将此变量设置为空白:
$config['index_page'] = '';
.htaccess 文件(删除 index.php):
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/ [L]
</IfModule>
根据 index.php
页面更改路线。如果删除 index.php:
<link rel="stylesheet" type="text/css" href="<?php echo base_url('/assets/css/styleshee.css');?>">
创建新文件夹并以您自己的名字命名。我创建了新文件夹调用 "assets"。然后将其放置在项目文件夹中,如下图所示。 image of the folder structure
然后我更改了 config.php 文件基础 url 路径。 image of the changed config.php file
之后我在控制器中添加了新的助手 class。 Image of the changed controller class