在 codeigniter 中设置基础 url

Set up the base url in codeigniter

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

 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>

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

现在我更新我的图像源和其他库源我删除本地主机和我的目录文件夹的名称:

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

但是我收到错误。它说找不到对象。有人可以帮我吗?

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

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

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

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

在Config.php

$config['base_url'] = 'http://localhost/Appsite/website/';
$config['index_page'] = '';

# If online site
# $config['base_url'] = 'http://whosebug.com/';

In .htaccess(应用程序文件夹外) - 删除 URL

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

访问URL

<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 助手

只要把它放在项目的自动正确路径和设置基础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;