Need of URL with username, similar to facebook


How would you do a facebook like “shortcut” to the user’s profile in YIi?

I’m thinking of having

The problem I see is that username1 is not a module, or an action or nothing, it’s a variable username.

I’m not very advanced in Yii.

All you need is the appropriate URL rule, that translates the query to e.g.

This is as easy as:





        '<user>'    => 'user/profile',

Note, that this rule matches pretty much anything. So if you have other, more specific rules, put them before this rule. For details see:




			'showScriptName' => false,


                                '<user:\w+>/<id:\d+>' => 'user/view',

				'<user:\w+>/contact' => 'user/contact',

				'<user:\w+>' => 'user/index',

hi mike,

i am trying to same thing. i am passing two arguments in URL 1.usename 2.user ID.

now i want to create Url like with the help of urlmanager.

but now i am not getting exactly what kind of rule i should place in my config file.

because when i use your suggested rule that doesn;t work for me. then i try


which make it clean url as i want but.that URL don’t work on the server and give a 400 error message.

i am bit confuse about writing a Url rules in Yii…would you please give me any suggestion how could i get rid off from this problem. ???

Sure that it’s not a 404? And who’s throwing the error? The webserver (indicating that mod_rewrite doesn’t work) or Yii (indicating that your URL rules still aren’t right).

Error 404

The system is unable to find the requested action "jayant14". like (username)(id)

but i want to display this url like

this error message i am getting after implementing this rule…mod_rewrite is working fine on my webserver excluding this all the rules which i define is working correctly…

this is code of my url manager






above two rules are working fine but 3rd one is not working for me.

since i am passing two arguments in my publicprofile function of user controller… and to make this url clean i define that rule…

would you please suggest me something…

  1. The rule ‘/<_a>’=>‘page/<_a>’ is already matching anything, so put this rule as last rule

  2. Your regular expression seems strange: name and id both can contain letters + numbers. So how should Yii know, where a name ends and the id starts? You should maybe use a different URL schema and put a / between them (/johnny/24).

I meet some problem using the





        '<user>'    => 'user/profile',

it sometimes throws 404 exception :( even if I put all the rules before it…

My next solution is to create a new behaviour to handle request processing in the application,

it uses the application’s catchAllRequest property (since I rarely use it :P)


class UsernameURLBehaviour extends CBehavior {

    public function attach($owner) {

        // set the event callback

        $owner->attachEventHandler('onBeginRequest', array($this, 'beginRequest'));



     * This method is attached to the 'onBeginRequest' event above.

     * */

    public function beginRequest(CEvent $event) {

        $route = Yii::app()->getUrlManager()->parseUrl(Yii::app()->getRequest());

        $exists = User::model()->exists('username = :username', array(':username' => $route));

        if ($exists && !is_array($event->sender->catchAllRequest)) {//if the route exists in username

            //catch all request to profile/view

            $event->sender->catchAllRequest = array(


                'username' => $route,





and then in the application config

    'behaviors' => array(

        'onbeginRequest' => array(

            'class' => 'application.components.UsernameURLBehaviour',



then in the controller ProfileController just load the username from $_GET[‘username’] or you could preload the model at the UsernameURLBehaviour and then save it

works like charm for me

well, one of the setback is to make a username blacklist so user couldn’t register with a username similar with one of your controller’s name

it seems little bit working now…!! thanks mike

but i have one more query…

sine i’ve already told you that i want to create url like

but after create some difference between name and id i am getting this kind of url

would you please tell me how could i remove this also from Url…to make my desire formate…

thanks petra for you reply on my post…but mike’s suggestion put me near to my solution…so i’ll prefer his suggestion at this moment…but your approach is also good. :rolleyes:

Please show again how your updated rules now look and which URL formats you want.

now i have my updated rules are look like this





then i am getting url look like this

and i want to display it like

i am passing two parameters in link first one is name and other one is id.

i hope this time you got everything what i am trying to say…

I’m sorry, i don’t understand. If you don’t want id in your URL, then why do you have it in the rule? You should then only use




[color="#1C2837"] [/color][color="#1C2837"][size="2"]Maybe your problem is that you also have this rule (as you showed above):[/size][/color]

[color="#1C2837"] [/color]




[color="#1C2837"][size=“2”] [/size][/color][color="#1C2837"]If you do have such a rule, then you’re out of luck: Both rules match anything, so how should Yii know, which rule should apply if a URL matches both? It will always use the first matching rule in this case.[/color]

yes, i think you are saying i am not passing argument two in link…just change code of my action this time. ;)

and i also remove




this code from my rules. now everything is working fine as i want. but my page controller actions are not working same as before.but thats not a big deal.i’ll manage it it. ;)

the main issue was that to create a url like that format and yes it is…so i just want to give a thanks for your support and help mike…once again Yii prove that YES IT IS RIGHT… :D

thanks again mike…!!! see you on some another forum topic…!! :)

Hi! How about the performance of the app?


matches with a lot of urls, I have to access to bbdd everytime finding the username.