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

I am working on a small project and all of my urls are written without base_url() function. So I was thinking what does those base_url() functions help with my urls. I know it will prefix the url based on configs file preferences. But simply I wanna ask
What is the advantage of using them ? and whats the disadvantage of not using them ?

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

Its needed every-where, or at least we should use everywhere. Do you know, absolute url is far better then relative url? So, when you are using an absolute urls for all links on your site, instead of hard coded root domain name, you can use the "base_url()" function. Moreover, CI reactor is smart enough to detect the original base url, so you even don't need to enter it in config file.

I usually using the base_url() for the correct paths to the static files (css, js, img) files and the site_url() for the links to pages. site_url() will return with url_suffix witch could be set in application/config/config.php file

Advantage:-if you not use base_url() and you want to switch directory/server of your site , you should change the path all over the project.
if you use base_url(), just change it at onece in config.php ,effect on all site.

Related

how to link to other website with codeigniter

I'm new to codeIgniter. Can you tell me, how to link to other website url in codeIgniter?
I tired to do like this:
google
But instead it gives me this
google
It always refers to the base url, even though I don't echo base_url(); on the href.
Thanks
When creating links to external pages, you must prefix the URL with either http:// or https:// , like this:
google
Technically this has little to do with CodeIgniter, but a difference between relative and absolute URLs.
If you did want to use CodeIgniter to generate the link, do this:
$this->load->helper('url');
echo anchor('http://www.google.com', 'Google');

Relative baseurl in magento

I am wanted to know that if there any settings in magento that we can remove fixed baseurl and keep dynamic(Relative) baseurl such as drupal.
It's not possible to have a relative base URL, but there's a good tutorial how to set base URL by an ini file in case of live/staging. You could adapt this to your needs:
http://maglife.co.uk/2009/03/28/magento-base-urls-and-devstaging-installations/
I would change it to read the base url from an Apache variable, which you could set per virtual host or directly read it from $_SERVER['HTTP_HOST'].
I have found that Magento sometimes will return an incorrect protocol (HTTP/HTTPS). To bypass this I've used hard-coded relative protocol URLs in certain cases (in phtml templates only) instead of calling functions.

Codeigniter anchor can't find link

I am using CodeIgniter to design a website, and I am having trouble getting anchor to work correctly.
Here is how I am using it. It is used in a file in views folder of application in codeigniter.
<?php echo anchor('/profile', 'PROFILE'); ?>
My intention is to have anchor load the profile controller, which is in the controllers of application in codeigniter. However, when I clink on the link, it says that the the file is not found.
I autoload the url helper functions alreader, and other functions from that file are working, like site_url() .
I don't know what I could have missed? Do you ave any suggestions? This is my second project with codeigniter, so I am still learning.
EDIT: Yes, I am following the naming convention in codeigniter, and the file has an index function. I tried without the forward slash and it still gives the same result.
The HTML link that this produces is localhost/profile. This is what I Should get right? Since for codeigniter, it's url/controller/function. I did a mod rewrite to remove index.php from the url, but that shouldn't be a problem, should it? I'll try and check the base url again.
Are you sure you need the leading forward slash?
Have you tried:
<?php echo anchor('profile', 'PROFILE'); ?>
You would only be using the slash if you are trying to get into the "profile" sub folder inside your controllers folder.
What helps me when urls get confusing is to type into the browser the full url to the path I want to get to (to make sure I'm not getting a 404, etc). From there you can start from the end and go back to see what you are missing.
But you generally want to start with a controller name and add the function you are calling like:
anchor(controllerName/functionName)
See CodeIgniter URL Helper for examples.
EDIT: also - you may need to check what your config base_url is set to. if there's fancy .htaccess stuff happening, your base_url needs to be set properly.
There is a very simple solution go to config/constants and define SITE_URL there and set its value to
http://localhost/myprojectname/index.php.
Then use it here like this.
<?php echo anchor(SITE_URL.'/profile', 'PROFILE'); ?>

CodeIgniter - Which function to use for URL's

Im building my first application using CodeIgniter, i need a bit of advice.
There are 2 functions that do the same thing and i was wondering which is the best to use.
Ok, so usually when im building a site, i link to the homepage by a simple / but if building on a directory, it will take back to the root public_html directory, with codeigniter i have found both
site_url()
and
base_url()
but, they both seem to do the same thing .. Just wondering if theres any difference, which one is better to use, etc etc.
Cheers,
If you are using index.php
site_url()
will include the index.php , and
base_url()
will not include it.
If you are creating a url to pass, use
site_url('images/img.png')
otherwise...
base_url().'images/img.png'

Magento Checkout - Unsecure Image

I was having a problem with my Magento checkout whereby the browser was reporting that the page wasn't fully encrypted. I had a look and realised that one of the images was using http://
This is the code I was using to display the image:
getSkinUrl('images/search_button.png') ?>
As a temporary solution I have hard coded the full path to the image ie:
https://mysite.com/skin/frontend/default/mytheme/images/search_button.png
Does anyone how I can call the image dynamically but have it use https on the checkout and other secure pages?
That should be solved by using the '_secure' parameter.
$this->getSkinUrl('images/search_button.png', array('_secure' => true));
Have you set your admin config settings to "use secure" (I can't remember the exact wording) for the frontend? If so, have you set your secure URLs to be HTTPS?
Magento generates image and link URLs via those secure/unsecure URLs you specify. If you haven't properly specified those as https://, you will have this problem. Otherwise, Magento is actually very good about outputting only secure content.
I haven't tested this for skin URLs, but for regular URLs you can do the following to force secure URLs. This is from the deep bowels of the URL code:
// Get the URL for another action on our current controller
// and force it to https
$path = "*/*/submit";
$url = Mage::getUrl($path, array('_forced_secure' => true));
There's probably some way to specify those extra arguments in getSkinUrl...

Resources