How to organize country/state/city browsing in cod

2019-01-29 00:13发布

问题:

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