CodeIgniter Forums
Newbie routing problem - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forum-20.html)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forum-23.html)
+--- Thread: Newbie routing problem (/thread-31306.html)



Newbie routing problem - El Forum - 06-14-2010

[eluser]RickP[/eluser]
I don't understand what routing is doing to me.

What I want is straight from the guide but I'm getting very strange results.

I have a controller 'division' with a function 'individual'. Whenever I access this function all the urls get the segments 'division/individual' added to them.

This means my menus no longer work. For example, instead of pointing to 'http://example.com/home' it now points to 'http://example.com/division/individual/home'.

My menu code has simply 'home' and that works fine until I access the 'division/individual' function.

What's going on?


Newbie routing problem - El Forum - 06-14-2010

[eluser]mddd[/eluser]
How are you coding your link exactly? If your link is
Code:
<a href="home">Home</a>
then it is logical. From the page /index.php, the browser will try to load /home. But from /some/page, it will try to load /some/page/home. Remember, the browser doesn't know you use CodeIgniter. It looks at the urls like normal paths!

The right thing to do is use CI's site_url function:
Code:
<a href="&lt;?=site_url('home')?&gt;">Home</a>
This will make the link relative to the root of the CI system. That way it always works, no matter where the browser thinks you are in the folder structure.


Newbie routing problem - El Forum - 06-14-2010

[eluser]Simian Studios[/eluser]
More than likely the problem is in your view rather than anywhere else - I am guessing that in your HTML you have links like this:

Code:
<a href="home">example</a>

If that's the case, its easy to fix, just make the links absolute by adding a preceding "/" e.g.

Code:
<a href="/home">example</a>

If not, post your view code and we should be able to work it out.


Newbie routing problem - El Forum - 06-14-2010

[eluser]Simian Studios[/eluser]
Heh, mddd and I have the same idea - either way works the same, it's just a matter of preference. I prefer to just write the link as it's what I'm used to, and saves a PHP function call, but it's no biggie..


Newbie routing problem - El Forum - 06-14-2010

[eluser]mddd[/eluser]
Simians way will work fine. But not if you change the location of the CI folder.
For instance, I have made a CI system that works in a subfolder of someone's website. And then the address is no longer "/".
That's why I think it's safer to use the site_url function. If the site address changes, you only have to change 1 line in the configuration file.


Newbie routing problem - El Forum - 06-14-2010

[eluser]Simian Studios[/eluser]
[quote author="mddd" date="1276545659"]Simians way will work fine. But not if you change the location of the CI folder.
For instance, I have made a CI system that works in a subfolder of someone's website. And then the address is no longer "/".
That's why I think it's safer to use the site_url function. If the site address changes, you only have to change 1 line in the configuration file.[/quote]

Good point, hadn't considered that..