A url manager question

How can i config the url manager to make user’s own page URL like “www.example.com/username”?

THX.

It will depend on the controller and action that you are using.

Let’s say, for the sake of argument, that you are using SiteController, and there is a user action to display the user homepage, then you would have something like:


'<:username\w+>' => 'site/user',

and your user action would take a $username argument from which you would then have to determine the user and display the appropriate content.

Note that you would have to take care with the position of your rule, because that kind of rule will override all others if positioned above them.

To create links to these pages, you would then use:


$this->createUrl('site/user', array('username' => $user->name));

assuming that you have loaded the appropriate User model and name is where you get the username value from.

If you want more flexibility, you can even do something like:


'<:username:\w+>/<controller:\w+>/<action:\w+>' => '<controller>/<action>',

providing you use the $username param in your actions, and specify it as a parameter when generating your urls.

Note that with the upcoming version 1.1.8 it’s possible to use custom url rule classes. You could create a UserRule class which connects to the database and checks whether the provided username is valid. This way it’s possible to have more than one rule with this “catch-all” schema:


'<username:\w+>' => 'site/user',

You can read more about it here: http://code.google.com/p/yii/source/browse/trunk/docs/guide/topics.url.txt?spec=svn3273&r=3236#342

I like the look of this upgrade - I think it will be very useful!

THX for your reply, and one more question, in which file shall I put


'<:username:\w+>/<controller:\w+>/<action:\w+>' => '<controller>/<action>',

code?

Sounds wonderful! Looking forward to the next version.

In your urlManager array in the main.php config file.