views:

40

answers:

1

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 functionality 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.

+2  A: 

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 funtionality:

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.

dflock
I didn't find any good way to do it through routing but you are probably right about mod_rewrite. Thanks!
Irro
Using routing is exactly to solve this sort of problem, and the CI user guide provides quite clear guidance on using it.
Kurucu
My bad, I dissed it way to fast. It was perfect for what I needed to do: $route['shop/([\\w]*)'] = "shop/search/$1";
Irro