How to organize country/state/city browsing in codeigniter url

孤者浪人 提交于 2019-11-30 09:44:14

问题


I'm creating a website that includes a country/state/city browsing interface. For SEO purposes, I want to include the name of the country/state/city in the URL. For example, the URL may include additional variables and would be something like:

http://www.domain.com/USA/NewYork?Category=car&color=black&Model=2009&page=5

http://www.domain.com/Spain/Allcity?Category=car&color=black&Model=2009&page=20

I need to fit this functionality into CodeIgniter. I'm not sure how to organize the major components, specifically the Controller, and possibly the Library class.

Is there a standard or best practice approach?


回答1:


Edit the routes.php file located in your config folder.

As noted in the link from Bora (CI URI Routing) Some examples from the documentation:

$route['journals'] = "blogs";
//A URL containing the word "journals" in the first segment will be remapped to the "blogs" class.

$route['blog/joe'] = "blogs/users/34";
//A URL containing the segments blog/joe will be remapped to the "blogs" class and the "users" method. The ID will be set to "34".

In your case you need to reconcile the names of the functions in your controllers and the desired URL names. You could end up with the examples that you show in your question, but you'd have to lay out a route for every country that you want in the first URI segment after domain.com. It's probably easier to do this:

http://www.domain.com/locations/USA/NewYork?Category=car&color=black&Model=2009&page=5

And then in your routes folder:

$route['locations/(.*)/(.*)'] = 'locations';

If you need to pass additional parameters to your controller via URL you can use query strings as you show above, or add

$route['locations/(.*)/(.*)/([0-9]{1,5})'] = 'locations/$1';
//assumes that parameter is numeric, 0-9 up to 5 digits


来源:https://stackoverflow.com/questions/24206065/how-to-organize-country-state-city-browsing-in-codeigniter-url

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!