Codeigniter - Is it ok to add functionality to the constructor in controllers?
I'm making a project where I want the user to search for shops in different cities and would like the url to be like this: domain/shop/city/name.
So I created a controller in codeigniter called Shop. But I cant create a city function since the city part of the url changes dependent on city name. One easy way to do it would be to add a function called "search" and add the functionality there but then I get url's like: domain/shop/search/city/name which I really would like to avoid.
So my question is if it's ok to add my functio开发者_开发百科nality directly into the constructor to avoid that extra "search" part in the url? I'm afraid that there might be some performance tricks involved that potentially keeps the class in memory so the constructor will not be called every time.
Codeigniter routing is fairly flexible - you can probably do what you need by fiddling with the settings in your routes config, possibly using the wildcard functionality:
- http://codeigniter.com/user_guide/general/routing.html
If not, you could always use Apache's mod_rewrite to seamlessly rewrite URL's from the domain/shop/city/name form to the domain/shop/search/city/name form, or some variation.
精彩评论