简体   繁体   English

在 codeigniter 中设置基本 url

[英]Set up the base url in codeigniter

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://localhost when accessing the images or libraries in code igniter.我认为在代码点火器中访问图像或库时包含http://localhost不是一个好习惯。 So I tried to change the $config['base_url'] in config.php to $config['base_url'] = "http://".$_SERVER["HTTP_HOST"]."/";所以,我试图改变$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?有人可以帮我吗?

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.php in URL.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]

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_url load URL helper from autoload.php从 autoload.php 使用base_url加载 URL 助手

In your config.php set 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”>

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;

in which file we have to put this ? 我们必须把它放在哪个文件中? ^ @Sumesh TG ^ @Sumesh TG

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM