When do we need to use base_url() function in codeIgniter? When do we need to use base_url() function in codeIgniter? codeigniter codeigniter

When do we need to use base_url() function in codeIgniter?


The main advantage is that; when you want to migrate your project to online or in a different server; you won't need to edit your hard-coded links. The base_url() will make your inside links to get their base url dynamically.

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


base_url is needed when you create login/signup with email verification or forgot password functionality (Verifying by clicking the links like this http://yourdomain.com/auth/userid/code).This is only one example, may have lot of uses.


the difference of base_url() and site_url() is that

both returns the url of your site but site_url() returns your site url, with attached index.php or on what you have configured $config['index_page'] = '' in application/config/config.php

example www.yoursite.com/chicken/turkey

when using base_url(): Returns your site base URL, as specified in your config file.you will get wwww.yoursite.com

when using site_url():you will get www.yoursite.com/index.php

site_url() is usefull if you haven't change your .htaccess and you still access your controllers with index.php

base_url() is usefull when linking your css,js,or media stuffs. Granting that you still use index.php when accessing your controller/method