php - Codeigniter URL 和 base_url() 混淆

标签 php html css codeigniter

 $config['base_url'] = 'localhost/project';

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

会产生错误,并且由于双“/”,它不会在我的 View 中加载我的 css。

这是浏览器上 View 的页面源。

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

并且 css 不会加载。当我尝试页面源并点击链接时,它会导致 404 错误。

令我感到困惑的是,当我在配置文件的 base_url 上添加协议(protocol)时。

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

由于未知的原因,上面的 url 似乎指向 css 并且它在我的 View 中加载,即使它在“project”和“css”之间的 url 上仍然有两个“/”。此外,即使我在 View 上的“css”之前删除了“/”,它也会加载,只要我在我的基本 url 上添加一个协议(protocol),即使没有协议(protocol),即使没有双“/”它仍然会赢加载 css。

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

我也有一些问题,

在我上面的配置基础 url 中,我从来没有在末尾放置一个“/”,所以为什么当我在我的 View 上使用函数 base_url 时它添加一个“/”。为什么会这样?

我的第二个问题是我网址上的“/”。

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

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

在我的浏览器上单击查看页面源上的 url 时的第一个链接,它仍然在 url 中有 Controller 。

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

虽然如果我在“css”之前添加“/”,它会删除 url 中的“元素”和“页面”。

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

我可以解决问题,只是我不知道它们为什么会发生,所以即使我可以加载我的 css,我也感觉不对,因为我不知道为什么。

最佳答案

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">

关于php - Codeigniter URL 和 base_url() 混淆,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40467956/

相关文章:

javascript - 淡出图像,新图像在图像缩略图库顶部淡出

php - 按日期过滤数据?

php - 如何在浏览器中在线查看PHP或Apache错误日志?

javascript - 嵌入的 HTML 表单被丢弃

html - 样式表的路径未在 WordPress 上更新?

javascript - Semantic-ui,粘在外容器上

android - Android 版 Chrome 无法正确显示 Google 网络字体

php - 将数据发送到 JSON 以实现数据库连接

php - 用于处理数据的 MySQL 与 Web 服务器

javascript - 麻烦造型扩展 polymer 元素(纸 slider )