开发者

Custom routing in code ingniter

I want to use codeigniter for an ecommerce project I'm working on but I think I need some custom routing and I'm not sure if this is possible. I want to be able to use this url:

http://myecom开发者_StackOverflow社区msite.com/store/mens

By default in CI this would call the mens function in the store class. What I actually want is it to call a generic function in the store class and feed in 'mens' as a paremeter. The reason for this is that this site needs to have a mens,womens and childrens section.

Is this possible?

Also When I get further down the line...i.e

http://myecommsite.com/store/mens/category1/category2

how do I make Ci work with this?


Simply define a custom route in application/config/routes.php

Something like, for your url http://myecommsite.com/store/mens

$route['store/(:any)'] = "store/customfunction/$1";

This way all requests will be mapped to your "customfunction" method, which takes the parameter "mens"

You might also want to consdere the __remap() function, which overrides the methods (as opposed to the routing, which overrides the whole URI) Quoting from the manual:

If your controller contains a function named __remap(), it will always get called regardless of what your URI contains. It overrides the normal behavior in which the URI determines which function is called, allowing you to define your own function routing rules.

So you can use a __remap() function in your controller store, and anything will be redirected to that. Any segments after the method name are passed into __remap() as second parameter, and you can use this array with call_user_func_array().
This could come in handy for your second examples of URI. Might be something like

function __remap('mymethod',$array = array())
{
  return call_user_func_array('mymethod',$array);
}

and in your method "mymethod" you pick the array element and do what you need to do

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜