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.
APPPATHis 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/pathsrarely 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: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
assetsorpublicto store them.If you really need to store assets in your application folder, then just drop all the relative paths. You don’t need them since
APPPATHis absolute:That should work from everywhere.