CodeIgniter Forums
odd relative path behaviour - Printable Version

+- CodeIgniter Forums (
+-- Forum: Archived Discussions (
+--- Forum: Archived Development & Programming (
+--- Thread: odd relative path behaviour (/thread-22317.html)

odd relative path behaviour - El Forum - 09-04-2009


I'm fairly new to CI but I'm loving it and have built most of my app without too much trouble. I've run across something odd, and I'm not sure if it's a bug or if I'm just missing something obvious, so I thought I'd post here for help (let me know if there's somewhere better to post!)

I have the following bit of code in my routes.php file:

$route['(:any)-reviews'] = "category/parent/$1-reviews";
It works great. If someone goes to, it calls my "category" controller, the "parent" function, and passes "movie-reviews" to the function as a variable. I call a view from that controller that displays great, and of particular note (you'll see below why), all of my relative urls for my images work just fine. An image like public_html/images/image1.jpg can be called in the view using src="image/image1.jpg". If I inspect an image it returns the proper url of ""

Now, I was messing with adding some additional information to my url (say, the number of movie reviews to return), and it works, but all of my image paths in my view go wonky.

So if I do something really basic like add this route (I'd normally use variables, but I'll just hardcode "shortlist" and "25" to show what I mean):

$route['(:any)-reviews/short_list'] = "category/parent/$1-reviews/25";

...everything works, I can add a second input to my controller that properly takes the "25", but all of my images fail, because now they all point to "".

Any idea what's happening here? I'd expect this behaviour maybe if I was using ../image/image1.gif, but if I'm using image/image1.gif, I expect it to be relative to the website's base url (if that makes sense).

I like relative paths and know I could make use of base_url() variables and such, but was just wondering why this was happening instead of just throwing my hands up in the air and doing it differently.

odd relative path behaviour - El Forum - 09-04-2009

Hi neomech and welcome to the CI forums.

What mechanism are you using, in your view, to load your images?

It sounds like you're using embedded img src calls - but you know CI provides img() in the HTML helper?

odd relative path behaviour - El Forum - 09-04-2009

CodeIgniter HTML Helper Manual

Wow, I didn't know about the HTML help in CI actually - I just fell in love with CI all over again Big Grin

Aside from that, as to the thread topic...I'm actually surprised by that behavior too. In the view are you just doing regular HTML markup like "img src="? That definately would not be the behavior I'd expect either...

odd relative path behaviour - El Forum - 09-04-2009

Hi guys, thanks for the feedback.

My images are currently just being called with regular old html in my view like so:
<img src="image/image1.gif">
I could try the html helper class, although I'd still like to know what's causing this odd behaviour.

odd relative path behaviour - El Forum - 09-05-2009

[quote author="neomech" date="1252136926"]
I could try the html helper class, although I'd still like to know what's causing this odd behaviour.

If you do the first part of that sentence, and the problem goes away, you'll have answered the second part.

IOW you're experiencing the joy of routes and the way CI pipes everything through index.php (no matter what the URL suggests) and consequently life just becomes much simpler once you let CI handle pathing. Eg. by using img().