Welcome Guest, Not a member yet? Register   Sign In
Can you assign a base url for a particular folder in your config file?
#1

[eluser]Zombie1[/eluser]
Hello all,

I've been working with CodeIgniter for close to a week and have a question. If my base url in the config file is:

$config['base_url'] = 'http://localhost/index.php/';

This base url works fine as my program runs successfully, however, is it possible to assign, for example, an image or CSS folder inside the config file?

The reason I'm asking this is I have a large program and inside this program there are numerous files. Here's a snippet of code:

Code:
<link rel="stylesheet" href="<?php echo base_url('media/css/stylesheet.css');

When my program runs it doesn't display the css correctly, however, when I change the path to:

Code:
<link rel="stylesheet" href="<?php echo base_url('../../media/css/shadowbox.css');

This works fine. I could change the path manually but there's too many instances as well as too many pages.

Is there any way I can create a new path inside the config file in order for it to point to the correct folder? As I said at the beginning I'm pretty new to CodeIgniter so I'm assuming the 'base_url()' function points to the base url in the config file. Thanks in advance.
#2

[eluser]joergy[/eluser]
I'm afraid You hit one of the major drawbacks of CI, towards IMHO.
A full qualified URL to a Controller's method of CI looks like
http://<domain>/base_dir/index.php/path/to/controller/method
"code" is invoked from within "index.php", so it's quite easy to address files to be included, relative to the position of "index.php". But this doesn't work for images, JavaScript, CSS or other files (=resources) referenced from within a loaded page in the browser. The browsers interpret the "/" in the Url, even those behind "index.php". Which is misguiding.
So You always have to use "absolute" paths or full qualified URLs to Your resources. "absolute" means relative to the DocumentRoot of Your website.

On the other hand Your base_url should be http://localhost (without the main entry point "index.php") if the system and application folders directly reside in the DocumentRoot. Then You can use base_url('media/css/stylesheet.css') in Your "output" if the folder media resides in the DocumentRoot folder too.

One of the improvements to CI would be, if they make the "segment separator" configurable, e.a. instead of "/" a "|".
I think the CI guy had chosen that separator because then it is relatively easy to define web server rewrites hiding things like the "index.php" or even some basic folder names. And probably because of their routing stuff...
#3

[eluser]InsiteFX[/eluser]
I made a helper to do things like that, just change to your paths.

Code:
// ------------------------------------------------------------------------

/**
* Helper asset_url()
*
* @access public
* @param string
* @return string
*/
if ( ! function_exists('asset'))
{
function asset($uri)
{
  $_ci = get_instance();
                           //    | change your path here
  return $_ci->config->base_url('assets/'.$uri);
}
}

Need more just copy and rename.
#4

[eluser]CroNiX[/eluser]
You could easily do it if you properly set your base_url. It should NOT contain index.php within it. "index.php" should only be in the "index_page" setting. Then you could just use base_url('css/style.css') which would create http://yoursite.com/css/style.css, and site_url('controller/method') would create http://yoursite.com/index.php/controller/method (it will automatically add index.php from the index_page setting).

http://ellislab.com/codeigniter/user-gui...elper.html




Theme © iAndrew 2016 - Forum software by © MyBB