CodeIgniter Forums
How to avoid using <?=base_url();?> in link href? - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23)
+--- Thread: How to avoid using <?=base_url();?> in link href? (/showthread.php?tid=26164)



How to avoid using <?=base_url();?> in link href? - El Forum - 01-07-2010

[eluser]shinokada[/eluser]
I am using <?=base_url();?> everywhere.

For example,

Code:
[removed][removed]

<link rel="stylesheet" href="<?= base_url();?>css/jqaccordion.css" />

or

<form method="post" action="<?=base_url();?>index.php/jquery/ajaxprocess/" id="ajaxform">
etc.

I have seen using without <?=base_url();?>.http://www.weblee.co.uk/2009/06/07/simple-jquery-ajax-with-codeigniter-part-1/

like
Code:
<form method="post" action="/jquery/processform/" id="myform">

How can I set it up?


How to avoid using <?=base_url();?> in link href? - El Forum - 01-07-2010

[eluser]huzzi[/eluser]
If you CI codes in the root folder then you don't have to use base_url() .. someone correct me if i'm wrong.


How to avoid using <?=base_url();?> in link href? - El Forum - 01-07-2010

[eluser]Rick Jolly[/eluser]
[quote author="huzzi" date="1262904051"]If you CI codes in the root folder then you don't have to use base_url() .. someone correct me if i'm wrong.[/quote]
Yes, if you use absolute urls (starting with a "/"). But that isn't portable since you can't be sure you'll never move to a subdirectory. You can use <base href="<?=base_url()?>"/> in the head and then make all your urls relative.


How to avoid using <?=base_url();?> in link href? - El Forum - 01-07-2010

[eluser]mcr_rm[/eluser]
Depends how you have your application set up. Personally I use Base url methods in my header include files as I pass all views through a template which includes the header and footer or individual calls to load the header and footer view files in other views. I then have at max around five base_url calls to include sme scripts.

The rest of the things you reference could be cleared up by using CI form helper to open your forms (automatically includes the correct path) and url anchor method for links etc.

http://ellislab.com/codeigniter/user-guide/helpers/form_helper.html
http://ellislab.com/codeigniter/user-guide/helpers/url_helper.html


If you are not using standard header includes etc for views then that is perhaps something to look at.

The method above with the single slash is the absolute path as mentioned by Rick and this is not helpful should you reuse your appication in a sub directory or need to scale in any way.


How to avoid using <?=base_url();?> in link href? - El Forum - 01-07-2010

[eluser]sharpe[/eluser]
use html base tag
Code:
<base href = "<?php echo base_url()?>" />
in the head section.