1

I've got problems with sorting out the paths in my app.

I'm using APPPATH function to read/write my images's string path in my db. The problem is that the APPPATH puts the function after the controller

here is my function for getting the css

<link rel="stylesheet" type="text/css" href="<?php echo '../' . APPPATH.'_data/css/style.css'; ?>" />

and when i go to http://localhost/project/index.php/home it works great css links to http://localhost/project/application/_data/css/style.css which is correct

but then I have this other controller

http://localhost/project/index.php/member/index/1

The css file path has to be

href="<?php echo '../../../' . APPPATH."_data/css/style.css"; ?>"

because other wise my link to the css goes to

http://localhost/project/index.php/member/application/_data/css/style.css

I'm using a simple template structure where i have my header/content/footer in an include sub-folder in views and I'm sending data to the content. Pretty standard stuff. I just don't get why the APPPATH does not work as if when called it gives me the PATH to the APPLICATION folder regardless the class that's been used.

1 Answer 1

4

APPPATH is really for system files, not frontend assets. It's the path to your application folder, and that's what it should be used for. ../relative/paths rarely work well in Codeigniter since they are relative from the current URL (not the view file). This is really a mess you're getting into.

Simple solution is to use base_url() instead, but if you want shorter paths, this is what I use:

define('BASE_URI', str_replace('index.php', '', $_SERVER['SCRIPT_NAME']));

In most cases, the result will be /, but this will account for CI installations in sub directories. For you, it should return /project/.

It's not recommended to keep static files like CSS and images mixed into your application files. Most people create a directory called assets or public to store them.

<link rel="stylesheet" href="<?php echo BASE_URI.'public/css/style.css'; ?>" />
<link rel="stylesheet" href="<?php echo base_url().'assets/css/style.css'; ?>" />

If you really need to store assets in your application folder, then just drop all the relative paths. You don't need them since APPPATH is absolute:

<link rel="stylesheet" href="<?php echo APPPATH.'_data/css/style.css'; ?>" />

That should work from everywhere.

2

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.