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?

+3


source to share


6 answers


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

+2


source


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.



+1


source


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

0


source


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.

0


source


I usually use base_url () for correct file paths of static files (css, js, img) and site_url () for page links, site_url () will return using url_suffix

witch can be set in application / config / config.php file

0


source


Advantage: If you are not using base_url () and you want to switch your site's directory / server, you must change the path throughout the project.

if you are using base_url () just change it in onece in config.php, effect on all sites.

0


source







All Articles