• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
URI Routing

#1
[eluser]_mtr[/eluser]
So, I've been playing around in CI for a little while now, but I'm sort of stumped when it comes to URI routing.

For the sake of a decent example, say I'm creating a site about cars. If a user wanted info on a 1995 Honda Civic, they would click through a manufacturer listing, then a list of all Honda models, then a list of all years in which the Civic was manufactured. The info the user was looking for would be found at http://www.example.com/manufacturers/honda/civic/1995.

I would need a Manufacturers controller with a general manufacturer lookup function, correct?

My app is that far; I can display all models produced by a manufacturer, but I can't go any deeper.

Could someone give me an outline of what the Manufacturer controller and routes.php file need to look like?

Thanks

#2
[eluser]_mtr[/eluser]
Anyone?

#3
[eluser]_mtr[/eluser]
uh

#4
[eluser]jedd[/eluser]
Spam .. ignore him, and he'll go away.


[quote author="_mtr" date="1251099599"]
... http://www.example.com/manufacturers/honda/civic/1995

I would need a Manufacturers controller with a general manufacturer lookup function, correct?[/quote]

Well, maybe not quite. It's tricky, as this is a contrived example. If you have a site about vehicles of all sorts, you may then have a controller called car (and another one for trucks, one for planes, etc). Remember, contrived example! Wink

So your URL would start www.example.com/car/ ...

You'd have a browse and a search method, probably, and they might lead you to a method that would show you details for a car - which is kind of where you were heading above. But your URL might look more like this:

www.example.com/car/show/honda/civic/1995

I'd suggest that you don't want to play with routes.php if you can avoid it. And you can usually avoid it.

If you think of a controller as representing or managing a resource, things might be easier. Manufacturer is actually an attribute of a car, I'd say, so you are probably going to paint yourself into a corner if you stick with a controller called manufacturer.

You might have a controller elsewhere that you use to maintain your list of manufacturers - to add, rename, delete them - but that's not what you're doing in this instance.

#5
[eluser]_mtr[/eluser]
OK, the car example was stupid. Let's just drop the pretense, here's what I'm doing:

It's an app that delivers basketball statistical info. For example, if I wanted info on the 1993 Chicago Bulls, I'd end up at www.example.com/teams/bulls/1993.

Here, I think a teams controller would be the right way to go (but what do I know). I'm not concerned about adding, deleting, or editing info, just displaying it. There's a list of teams, click through for a list of seasons for a particular team, click through again for info on an individual season for a team.

My teams controller has a lookup function that pulls a team's seasons from the DB (www.example.com/teams/bulls), but I can't figure out how to pass the season as a variable, thus producing the info for the individual season.

I hope this makes some sort of sense.

#6
[eluser]jedd[/eluser]
Okay .. well, here's what I might do (I use that caveat because I'd probably ponder it for a while, and there's obviously a lot more to it than you can describe in a few paragraphs).

I'd have a controller called Team.

I'd have a method in there called show

That method would be declared like this:
Code:
function  show  ( $team_name = NULL , $year = NULL )

This means that if you took a URL like this: http://www.example.com/team/show/bulls/1993 then your $team_name would be set to 'bulls' and $year would be set to 1993.

I often try to think of the first parameter (the controller name) as the noun, and the second (the method or function) as the verb. And after that you just try to start with the most-significant-data and ascend down through data of lesser significance.

You sometimes find you want to set up a pairing approach - url's that end up with, say, ../name/bulls/year/1993 ... but that's a bit more complex, and works better when you have a large set of *possible* variables you want to transfer, but don't usually send all variables around. With your situation, context is fairly consistent. If this paragraph doesn't make sense, feel free to ignore it.

#7
[eluser]_mtr[/eluser]
I like it.

The thing I don't like, though, is the "show" method in the URL. I could use some regex in routes.php to hide that, though, right?

#8
[eluser]jedd[/eluser]
[quote author="_mtr" date="1251183506"]I like it.

The thing I don't like, though, is the "show" method in the URL. I could use some regex in routes.php to hide that, though, right?[/quote]

You could use the word 'view' instead, if you think that has better feng shui.

If you want to remove it entirely .. wow, you must really think your users are easily befuddled.

Would your team controller do anything other than this one thing? Ever?


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2020 MyBB Group.