【问题标题】:Codeigniter URL and base_url() confusionCodeigniter URL 和 base_url() 混淆
【发布时间】:2016-11-07 23:04:39
【问题描述】:
 $config['base_url'] = 'localhost/project';

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

会产生错误,并且由于双“/”,它不会在我的视图上加载我的 CSS。

这是浏览器上视图的页面源。

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

由于未知的原因,上面的 url 似乎指向 css,它加载到我的视图中,即使它在 'project' 和 'css' 之间的 url 上仍然有双 '/'。此外,即使我在视图上的“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 加载但我感觉不对,因为我不知道为什么。

【问题讨论】:

    标签: php html css codeigniter


    【解决方案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">
    

    【讨论】:

      【解决方案2】:

      理想情况下,配置中的 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">
      

      【讨论】:

        【解决方案3】:

        您根本无法使用主机名开始 URL:

        localhost/project
        

        否则,将无法判断您是指域名还是服务器中的位置。

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

        http://localhost/project
        

        ...或者,如果已经在 HTTP 的上下文中,至少是双斜杠:

        //localhost/project
        

        浏览器通常更喜欢隐藏实际的 URL(因此造成广泛的混淆),但底层值的工作方式完全相同。

        【讨论】:

        • 由于某种原因,chrome 隐藏了http://,但将https:// 显示为绿色并带有锁。
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-09-17
        • 2018-07-26
        • 2016-09-17
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多