views:

16

answers:

2

Hi. I'm developing a game fansite to learn CodeIgniter that includes a database of character information. I want to use this URL format:

http://www.mydomain.com/characters/joe
http://www.mydomain.com/characters/sam

So I have a controller called characters. If I understand the CI documentation, I can only parse "joe" and "sam" in the URL if I call a function. For example, say I define function "detail" and pass it the 1st URL segment. It would look like this:

http://www.mydomain.com/characters/detail/joe
http://www.mydomain.com/characters/detail/sam

I don't want that unnecessary depth in the URL hierarchy. Surely there must be some way to bring up detailed pages for "joe" and "sam" through the "detail" function in the "characters" controller without requiring "detail" to be in the URL, or to skip "detail" function alltogether. How do I do that?

A: 

You can check 1.The URL helper for uri_string() that will return the URI segments.

2.The URI routing write an extra controller e.g. Detail and then into your routes.php add

$route['characters/detail'] = "detail";

However if you want to automatically show the characters/detail view you can put everything into Index() and pass your variables from the controller to the view.

btrandom
Thanks, that got me far enough to figure out what I really wanted. My routes says:$route['characters/(:any)'] = "champions/detail/$1";$1 passes to the detail function of the controller which uses it to query the database, pass to the views etc. The URL mydomain.com/characters/sam returns what mydomain.com/characters/detail/sam used to do. Thanks!
Michael Hopkins
A: 

You can have a function that accepts 1 (or many arguments). THese act as queryparameter.

So in your case, detail would be the function and sam|joe would be the first argument passed. In your code, you would do it the following:

public function detail($player) {
   //some code here, don't foget to check for a valid and present $player
}
DrColossos