Welcome Guest, Not a member yet? Register   Sign In
How to differentiate between "types" of URI segments
#1

[eluser]FragPacket[/eluser]
Greetings all,

I'm having an issue with segmented URIs... again. I've previously been using uri_to_assoc() to grab values, and it works fine as long as I'm working with a function within my controller. As soon as I use a controller with only a key/value pair, I get a 404 error.

For example, the following URI works fine:

Code:
http://www.mysite.com/mylinks/edit/linknum/1004

but this one gives the 404:

Code:
http://www.mysite.com/mylinks/sortby/date


Which makes sense -- in the first case "edit" is a function in my "mylinks" controller. In the second case, "sortby" and "date" are a key/value pair.

Is there a way to force my controller to assume control of the situation and just ignore the "/sortby/date" for routing purposes??


Thank you!
#2

[eluser]Herb[/eluser]
First of all you need to reread URL's in the manual. Your first example works because it follows the convention of controller/method; whereas, your second example does not. Codeigniter can not find a method called in sortby in the controller mylinks and therefore throws the 404.

I assume you are really trying to pass sortby date as a parameter to the index method of the mylinks controller. Correct? Then you will need to use Routing from the manual to direct it to the correct method and then extract segment 2 and 3 as the parameter.

Hope this helps.
#3

[eluser]FragPacket[/eluser]
Thanks Herb -- you're right, of course. I was just cranky because I think I'm allergic to segmented URLs.

What I had meant to say was that I wanted to pass a key/value pair to the default (index) function within a controller, without invoking it explicitly. I do get the normal "controller/function/key/value/key/value" relationship, but frustration was getting the better of my stating such.

I'm going to spend some time with adjusting my routes for this and will post again with my fix.

Again, thanks.
#4

[eluser]FragPacket[/eluser]
Solution:

In routes.php I added the following line:

Code:
$route['mylinks/sortby/(:any)'] = 'mylinks/index/sortby/$1';


Default method called, and no one's the wiser. Smile
#5

[eluser]Herb[/eluser]
Yea, sometimes I need to do that when it seems to be redundant to have addition item in the uri; such as a /stories/story/title.

I would have tossed you the answer; but, I've learned that if I'm pushed in the right direction and then come up with a solution, the solution sticks with me. Otherwise, I find myself asking the same question over and over.

Glad it worked out.

Herb




Theme © iAndrew 2016 - Forum software by © MyBB