Question About Changing Url Parameter

Hi all,

Have been looking at some PHP frameworks, and chose Yii as the one I want to learn. I am using version 1.1.14

What I want to do is the following:

I have a database table that stores an inventory of about 300 different cars.

To view the entire inventory, you would go to [color="#000080"]mysite.com/inventory[/color]

From there, you would choose a vehicle to view more detailed information about the selected vehicle.

When you select a vehicle, you get redirected to [color="#000080"]mysite.com/inventory/1[/color]

What I want to do is change the "1" at the end of the above URL (which of course is the ID of the vehicle selected) to something like this instead: [color="#000080"]mysite.com/inventory/corvette[/color]

After searching, I’ve found that I would need to go to config/main.php and edit the urlManager rules. But that is where I am stuck. I am not sure how to change the url so that it displays the name of the car instead of the id of the car at the end of the URL.

Any help would be greatly appreciated!

is the car name unique in your DB table ?

No, there is potential for there to be more than one car name. In fact, there could be multiple cars of the same year / make / model (well, in theory at least…it is best to prepare for that just in case), which really seems to limit how I can adjust the URL.

How would I go about this if I have a unique field? I have other tables I would like to do this to as well, and most of them have unique field names

Let’s say you have no url rules.

With your example, assuming Inventory is the controller, that means that inventory/index (index action) should show you the list of cars, and inventory/view/?id=1 (view action) should show you the car with ID = 1.

As you said, you don’t want to get the car by ID, you want to get it by name, so the name can appear in the URL, which is more friendly: inventory/view/?id=corvette

BUT, if name is not unique, then you should make sure that the ID parameter can be unique, and it can grab the required field from your DB. One way you can do this is append the ID to the name: inventory/view/?id=1-corvette . This parameter is actually called a "slug". So inventory/view/?slug=1-corvette

Now, in your view action, in the controller, you will see that the default implementation calls a function loadModel($id). Well, you will have to modify that action, extract the id (1) first from the $_GET[‘slug’] param in the URL, and then call the function loadModel(1) to load your model from the DB.

As for URL rules, you want to map inventory/1-corvette to inventory/view/?slug=1-corvette. So the rule is:


'inventory/<slug:[a-zA-Z0-9-]>' => 'inventory/view'

Which means whenever you access inventory/someSlugwithIDandName your app will take you to inventory/view and append the ‘slug’ param via GET, and it will be available in view action via $_GET[‘slug’].

So for inventory/1-corvette , it will go to actionView in InventoryController , and $_GET[‘slug’] = ‘1-corvette’; Extract ID, load model by ID, show the model, done.