php 在 codeigniter 中设置基本 url

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/38122186/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 01:29:58  来源:igfitidea点击:

Set up the base url in codeigniter

phphtmlcodeignitercodeigniter-2base-url

提问by qazzu

I have the directory structure like this in code igniter:

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

 Appsite
    -website
       -application
        -images

When I accessing the image in index.php I used: <img src="http://localhost/Appsite/website/images/images.PNG"

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

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

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

I think it is not a good practice to include the http://localhostwhen accessing the images or libraries in code igniter. So I tried to change the $config['base_url']in config.phpto $config['base_url'] = "http://".$_SERVER["HTTP_HOST"]."/";

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

And now I update my image source and other library source I remove the localhost and the name of my directory folder:

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

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

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

But I get errors. it says object not found. Can some help me?

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

回答by Abdulla Nilam

In Config.php

在 Config.php

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

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

In .htaccess(outside application folder)- To remove index.phpin URL

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

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


To accessing URL

访问网址

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

To access image

访问图像

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

To access CSS

访问 CSS

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


To use base_urlload URL helper from autoload.php

使用base_urlautoload.php 中的加载 URL 助手

回答by Vinod VT

In your config.phpset the base_url()as,

在你的config.php设置base_url()中,

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

In your view load the image as,

在您的视图中加载图像,

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

回答by Jafarali Maknojiya

Just put this it will take the automatically correct path of the project and set base 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;