Need of URL with username, similar to facebook

Hi,

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

I’m thinking of having www.mysite.com/username1

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.

www.mysite.com?r=user/profile&user=username1

This is as easy as:


'urlManager'=>array(

    'urlFormat'=>'path',

    'showScriptName'=>false,

    'rules'=>array(

        '<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:

http://www.yiiframework.com/doc/guide/topics.url




'urlManager'=>array(

			'urlFormat'=>'path',

			'urlSuffix'=>'.html',

			'showScriptName' => false,

			'rules'=>array(


                                '<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 www.mydomian.com/username 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

‘<username:[a-zA-Z0-9_-]+>’=>‘user/profile’,

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

www.mydomain.com/jayant

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

‘rules’=>array(

	        '/'=&gt;'site/index',


	        '/&lt;_a&gt;'=&gt;'page/&lt;_a&gt;',


	        '&lt;name:[a-zA-Z0-9_-]+&gt;&lt;id:[a-zA-Z0-9_-]+&gt;'=&gt;'user/publicprofile',


	    ), 

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




'urlManager'=>array(

    'urlFormat'=>'path',

    'showScriptName'=>false,

    'rules'=>array(

        '<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)




<?php


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(

                'profile/view',

                '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 www.mydomian.com/name

but after create some difference between name and id i am getting this kind of url www.mydomian.com/name?id=integernumber

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

‘rules’=>array(

	        '/'=&gt;'site/index',


	        '&lt;name:[a-zA-Z0-9_-]+&gt;/&lt;id:[0-9_-]+&gt;'=&gt;'user/publicprofile',


	    ),

then i am getting url look like this www.mydomain.com/name/id

and i want to display it like www.mydomain.com/name

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"][size="2"]


'<name:[a-zA-Z0-9_-]+>'=>'user/publicprofile',

[/size][/color]

[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="3"]


'/<_a>'=>'page/<_a>',

[/size][/size][/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 right.now i am not passing argument two in link…just change code of my action this time. ;)

and i also remove

[color="#1C2837"][size="2"][size="3"]


'/<_a>'=>'page/<_a>',

[/size][/size][/color]

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?

‘<name:[a-zA-Z0-9_-]+>’=>‘user/publicprofile’,

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