When do we need to use the base_url () function in codeIgniter?
I am working on a small project and all my urls are written without a function base_url()
. So I thought these functions were base_url()
helping with my urls. I know it will be url prefix based on the settings of the preferences files. But I just want to ask
What is the advantage of using them? and what is the disadvantage of using them?
source to share
The main advantage is that; when you want to transfer your project online or to another server; you won't need to edit your hardcoded links. Base_url () will force your internal links to get a dynamic base url.
echo base_url("blog/post/123");
will return
http://example.com/blog/post/123
in example.com
and will return
http://jms.com/blog/post/123
in jms.com
source to share
base_url
required when creating login / registration with email authentication or forgot password feature (verification by clicking on links like this http://yourdomain.com/auth/userid/code ). This is just one example and can have many uses.
source to share
difference base_url()
and site_url()
is what
both return your site url but site_url()
return your site url with the attached index.php
or whatever you configured $config['index_page'] = ''
inapplication/config/config.php
example www.yoursite.com/chicken/turkey
when used base_url()
: returns the base url of your site as specified in your config file. You'll getwwww.yoursite.com
when using site_url()
: you will getwww.yoursite.com/index.php
site_url()
useful if you haven't changed yours .htaccess
and you still access your controllers withindex.php
base_url()
useful when linking your css, js or media. Providing what you are still using index.php
when accessing your controller / method
source to share
It is needed everywhere, or at least we should use it everywhere. Did you know that an absolute url is much better than a relative url? Thus, when you are using absolute URLs for all links on your site, instead of the hard-coded root domain name, you can use the "base_url ()" function. Also, the CI Reactor is smart enough to detect the original base url, so you don't even need to enter it in the config file.
source to share