开发者

How do I avoid having to go two levels deep in CodeIgniter before I can parse URL segments?

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. F开发者_C百科or 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?


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.


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
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜