what is the difference between site_url() and base_url()?
echo base_url(); // http://example.com/website
echo site_url(); // http://example.com/website/index.php
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.
public function site_url($uri = '')
{
if (empty($uri))
{
return $this->slash_item('base_url').$this->item('index_page');
}
$uri = $this->_uri_string($uri);
if ($this->item('enable_query_strings') === FALSE)
{
$suffix = isset($this->config['url_suffix']) ? $this->config['url_suffix'] : '';
if ($suffix !== '')
{
if (($offset = strpos($uri, '?')) !== FALSE)
{
$uri = substr($uri, 0, $offset).$suffix.substr($uri, $offset);
}
else
{
$uri .= $suffix;
}
}
return $this->slash_item('base_url').$this->slash_item('index_page').$uri;
}
elseif (strpos($uri, '?') === FALSE)
{
$uri = '?'.$uri;
}
return $this->slash_item('base_url').$this->item('index_page').$uri;
}
Base URL function.
public function base_url($uri = '')
{
return $this->slash_item('base_url').ltrim($this->_uri_string($uri), '/');
}
Difference betwwen CodeIgniter Site Url and base Url
To clear all your doubts you have to read CodeIgniter Manual.Please Click Here
base URL
Returns your site base URL, as specified in your config file. Example:
echo base_url();This function returns the same thing as site_url, without the
index_page or url_suffix being appended.Also like site_url, you can supply segments as a string or an array.
Here is a string example: echo base_url("blog/post/123");
site URL
Returns your site URL, as specified in your config file. The index.php
file (or whatever you have set as your site index_page in your config
file) will be added to the URL, as will any URI segments you pass to
the function, and the url_suffix as set in your config file.You are encouraged to use this function any time you need to generate
a local URL so that your pages become more portable in the event your
URL changes.Segments can be optionally passed to the function as a string or an
array. Here is a string example: echo site_url("news/local/123");The above example would return something like:
http://example.com/index.php/news/local/123Here is an example of segments passed as an array: $segments =
array('news', 'local', '123');echo site_url($segments);
How do I access site url or base url from codeigniter controller?
Load URL Helper
and function site_url()
This helper is loaded using the following code:
$this->load->helper('url');
echo site_url();
site_url()
Returns your site URL, as specified in your config file. The index.php
file (or whatever you have set as your site index_page in your config
file) will be added to the URL, as will any URI segments you pass to
the function, and the url_suffix as set in your config file.You are encouraged to use this function any time you need to generate
a local URL so that your pages become more portable in the event your
URL changes.Segments can be optionally passed to the function as a string or an
array. Here is a string example:echo site_url("news/local/123");
The above example would return something like:
http://example.com/index.php/news/local/123
base_url() and site_url() not working in live server
Things working on localhost and not working on live linux hosts is 99% due to case of controller names , paths etc.
When you develop on Windows or MACs, they don't care about case. Linux does.
So if your Live Host is linux based...
Your welcome controller should be lower case in the URL.
As an aside: It's generally normal to use lowercase for naming methods ie insert_value or camel case insertValue. But that shouldn't be a show stopper - it's more a coding style convention. CI recommends lowercase using underscores for word separation ie insert_value.
<?php echo base_url();?>index.php/welcome/InsertValue
Where your Welcome Controller name in the URL is welcome (All Lowercase) and NOT Welcome.
Plus always LOOK at the generated Links etc in your Pages Source to make sure it looks ok! From your browser. That's usually a Right Mouse Click - View Source (dependant upon the browswer you are using ). Or you can F11 and view the source in the Panel or Right Click Inspect Element... You need to know how to Look at what's being generated and to try things out when you get stuck.
Hope that helps.
Difference between using $base_url and not using it on CodeIgniter
The difference is if you ever want to put your application in a subdirectory.
If what was / now becomes /application/ all your site root references will break.
It just makes your application free to be installed anywhere and won't require being on the site root.
Related Topics
Syntax Error While Defining an Array as a Property of a Class
Laravel 4: How to "Order By" Using Eloquent Orm
How to Get File Url Using Storage Facade in Laravel 5
A Better Approach Than Storing MySQL Password in Plain Text in Config File
Regex to Detect Invalid Utf-8 String
How to Get Primary Key of Table
PHP Access Network Path Under Windows
Routing Controllers in Subfolders Using Codeigniter
PHP Share Variable Among Different Users/Sessions
Laravel 5.2 Not Reading Env File
Sqlstate[Hy000] [1698] Access Denied for User 'Root'@'Localhost'
Run Cron Job on PHP Script, on Localhost in Windows
How to Convert Ipv6 from Binary for Storage in MySQL
Why Does 1234 == '1234 Test' Evaluate to True