【问题标题】:Set up the base url in codeigniter在 codeigniter 中设置基本 url
【发布时间】:2021-08-22 02:03:00
【问题描述】:

我在代码点火器中有这样的目录结构:

 Appsite
    -website
       -application
        -images

当我访问 index.php 中的图像时,我使用了: <img src="http://localhost/Appsite/website/images/images.PNG"

href 是: <li class=""><a href="http://localhos/tAppsite/website/index.php/home/">Home</a></li>

我认为在代码点火器中访问图像或库时包含http://localhost 不是一个好习惯。所以我试图改变config.php中的$config['base_url']$config['base_url'] = "http://".$_SERVER["HTTP_HOST"]."/";

现在我更新了我的图像源和其他库源,我删除了 localhost 和我的目录文件夹的名称:

<img src="images/images.PNG”> <li class=""><a href= <?php echo base_url;?> /website/index.php/home/">Home</a></li>

但我得到了错误。它说找不到对象。有人可以帮助我吗?

【问题讨论】:

    标签: php html codeigniter codeigniter-2 base-url


    【解决方案1】:

    在您的config.php 中将base_url() 设置为,

    $config['base_url'] = 'http://localhost/projectname/';
    

    在您的视图中,将图像加载为,

    <img src="<?php echo base_url();?>images/images.PNG”>
    

    【讨论】:

      【解决方案2】:

      在 Config.php 中

      $config['base_url'] = 'http://localhost/Appsite/website/';
      $config['index_page'] = '';
      
      # If online site
      # $config['base_url'] = 'http://stackoverflow.com/';
      

      .htaccess(应用程序文件夹外)中 - 删除 URL 中的 index.php

      RewriteEngine on
      RewriteCond $1 !^(index\.php|assets|image|resources|robots\.txt)
      RewriteCond %{REQUEST_FILENAME} !-f
      RewriteCond %{REQUEST_FILENAME} !-d
      RewriteRule ^(.*)$ index.php?/$1 [L,QSA]
      

      访问网址

      <a href="<?php echo base_url();?>contollerName/methodName"> click here</a>
      

      访问图片

      <img src="<?php echo base_url();?>images/images.PNG”>
      

      访问 CSS

      <link rel="stylesheet" type="text/css" href="<?php echo base_url();?>assets/css/style.css"/>
      

      要使用base_url 从 autoload.php 加载 URL 帮助程序

      【讨论】:

      • 从 autoload.php 使用 base_url 加载 URL 助手是什么意思?
      • 如果你使用base_url 它会给你错误。所以去config/autoload.php并在帮助程序中加载URL
      • 你的意思是这样$autoload['helper'] = array('form','url');
      • @qazzu 就是这样。 (y)
      • @qazzu 乐于助人 ;)
      【解决方案3】:

      只要放这个,它就会自动获取项目的正确路径并设置基本 URL

      $site_url = ((isset($_SERVER['HTTPS']) && strtolower($_SERVER['HTTPS']) !== 'off') || (isset($_SERVER['HTTP_X_FORWARDED_PROTO']) && $_SERVER['HTTP_X_FORWARDED_PROTO'] == 'https')) ? 'https' : 'http';
      $site_url .= '://' . (isset($_SERVER['HTTP_HOST']) ? $_SERVER['HTTP_HOST'] : '');
      $site_url .= str_replace(basename($_SERVER['SCRIPT_NAME']), '', $_SERVER['SCRIPT_NAME']);
      
      $config['base_url'] = $site_url;
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2020-06-19
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-07-29
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多