2016-11-07 63 views
-1
$config['base_url'] = 'localhost/project'; 

<link href="<?= base_url(); ?>/css/bootstrap.min.css" rel="stylesheet"> 

会产生一个错误,它不会因我的视图加载我的css因为双'/'。Codeigniter的URL和base_url()混淆

这是浏览器视图的源代码。

<link href="localhost/project//css/bootstrap.min.css" rel="stylesheet"> 

而且css将不会加载。当我尝试页面源并按照链接,它会导致404错误。

我很困惑的是,当我在配置文件上的base_url上添加协议。

<link href="http://localhost/project//css/bootstrap.min.css" rel="stylesheet"> 

不知什么原因,上述网址似乎指向css和它装载在我看来,即使它仍然具有双重“/”的“项目”和“CSS”的网址。另外,即使我在视图上的'css'之前删除'/',它也会加载,只要我在我的基础URL上添加协议,即使没有协议即使没有双'/'它仍然赢得' t加载CSS。

<link href="http://localhost/project/css/bootstrap.min.css" rel="stylesheet"> 

我也有一些问题,

在我上面的配置基本URL,我从来不把一个“/”末为何还加一个“/”当我在使用功能BASE_URL我的看法。这是为什么?

我的第二个问题是,我的网址上的'/'。

<link href="css/bootstrap.min.css" rel="stylesheet"> 

<link href="/css/shop-homepage.css" rel="stylesheet"> 

第一个链接,当在浏览器上点击视图页面上的url时,它仍然在url中有控制器。

http://localhost/project/Pages/css/bootstrap.min.css 

而如果我的“CSS”它消除了“项目”,并在URL“页数”前面加上“/”。

http://localhost/css/shop-homepage.css 

我可以解决这个问题,只是我不知道为什么会发生这样即使我能得到我的CSS加载我觉得这样不对,因为我不知道为什么。

回答

0

理想的情况下你在配置BASE_URL应该是这样的:

$config['base_url'] = "http://www.example.com/"; 

,并在你看来,你可以添加你需要包括文件夹名和文件名。

<link href="<?= base_url(); ?>css/bootstrap.min.css" rel="stylesheet"> 

这样就不会有混乱或双 '/'

加更喜欢使用这样的:

<link href="<?php echo base_url(); ?>css/bootstrap.min.css" rel="stylesheet"> 
1

base_urlURL Helper的一部分。

的问题是在我们的配置文件,配置文件说:

/* 
|-------------------------------------------------------------------------- 
| Base Site URL 
|-------------------------------------------------------------------------- 
| 
| URL to your CodeIgniter root. Typically this will be your base URL, 
| WITH a trailing slash: 
| 
| http://example.com/ 
| 
| If this is not set then CodeIgniter will guess the protocol, domain and 
| path to your installation. 
| 
*/ 
$config['base_url'] = 'http://localhost/site/'; 

你应该已经添加了斜线,因为你不这样做,base_url()功能会自动添加它。

的最佳用途是提供文件的位置作为一个参数,作为

<link href="<?= base_url("css/bootstrap.min.css"); ?>" rel="stylesheet"> 
0

你根本不能与主机名开始的URL:

localhost/project 

否则,它会无法分辨您的意思是您的服务器中的域名还是位置。

你需要一个完整的协议前缀:

http://localhost/project 

...或者,如果已经在HTTP的情况下,至少双斜杠:

//localhost/project 

浏览器常常喜欢隐藏实际的网址(因此普遍存在混淆),但其基本价值完全相同。

+0

由于某些原因,Chrome会隐藏'http://',但会显示绿色的'https://'并带有锁定。 – Phiter