Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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"

And the href is: <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. So I tried to change the $config['base_url'] in config.php to $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:

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

like image 987
qazzu Avatar asked Jun 30 '16 11:06

qazzu


People also ask

How base URL is defined in CodeIgniter?

Base URL should be absolute, including the protocol: $config['base_url'] = "http://somesite.com/somedir/"; If using the URL helper, then base_url() will output the above string.

What is the basic CodeIgniter URL structure?

The basic URL structure is bestinterviewquestion.com/class/function/ID . Here class represents controller's class that needs to be invoked. Here "function" is name of method which is called. ID is an additional parameter that is passed to controllers.

What is the difference between base URL and site URL in CodeIgniter?

if you want a URL access to a resource (such as css, js, image), use base_url() , otherwise, site_url() is better. for a detailed reference Check this both function in CodeIgniter. Base URL function. You may view his answer in the given link in my comment - the bold difference about index.


2 Answers

In 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

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

<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

like image 63
Abdulla Nilam Avatar answered Nov 18 '22 06:11

Abdulla Nilam


In your config.php set the base_url() as,

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

In your view load the image as,

<img src="<?php echo base_url();?>images/images.PNG”>
like image 27
Vinod VT Avatar answered Nov 18 '22 04:11

Vinod VT