Jump to content
Larry Ullman's Book Forums

Custom Url Management - City Names


joeschmoey
 Share

Recommended Posts

So I'm just starting out with Yii and I'm trying to figure out the best way to organize my project. It's largely focused on being local, so I want to have different sections of the site for different cities. I have a city table in my database and I want to organize my URL as such: www.example.com/city-name. For example, if I want to show Chicago, it would be www.example.com/chicago.


Right now I have a City controller and my cities are all attached to numeric id's. So my Chicago city (id = 1) URL reads as www.example.com/city/1. I want to get rid of my city controller name in the URL and use the name of the city instead of the id of the city in the URL.


I'm assuming the answer lies in this part of the documentation (yes I've tried looking at the documentation): http://www.yiiframework.com/doc/guide/1.1/en/topics.url#using-custom-url-rule-classes - but I'm too new at Yii to decipher that code.


First of all, is this the best way to organize my site and secondly, how do I get my URLs to look the way I want?


Thanks so much - I've been struggling with this for a couple weeks. Sorry I'm a noob.

Link to comment
Share on other sites

Using www.example.com/cityname makes a lot of sense, but only if the site will never deal with two cities with the same name. You'll need to account for that possibility (perhaps). 

 

The other problem is that you'll have to distinguish between /cityname and /anything else (e.g., /contact). Personally, I'd be inclined to think seriously about using /city/cityname. 

 

In either case, you'll need to route your rules to watch for city names (those characters that can be in a city's name). You'll also need to change your controller actions to load model data by the city name, not by the PK (which is the default).

Link to comment
Share on other sites

 Share

×
×
  • Create New...