Username Instade Id In Url

I am using "yii user" (modules/user), now the user url is - mysite.com/user/user/view/id/1

and i want change to - mysite.com/profile/username

User controller is -


 public function actionView()

    	{

    		$model = $this->loadModel();

    		$this->render('view',array(

    			'model'=>$model,

    		));

    	}

    

    public function loadModel()

    	{

    		if($this->_model===null)

    		{


    			if(isset($_GET['id']))

    				$this->_model=User::model()->findbyPk($_GET['id']);

    				

    			if($this->_model===null)

    				throw new CHttpException(404,'The requested page does not exist.');

    				

    		}

    		return $this->_model;

    	}

    

    

    	/**

    	 * Returns the data model based on the primary key given in the GET variable.

    	 * If the data model is not found, an HTTP exception will be raised.

    	 * @param integer the primary key value. Defaults to null, meaning using the 'id' GET variable

    	 */

    	public function loadUser($id=null)

    	{

    		if($this->_model===null)

    		{

    			if($id!==null || isset($_GET['id']))

    				$this->_model=User::model()->findbyPk($id!==null ? $id : $_GET['id']);

    			if($this->_model===null)

    				throw new CHttpException(404,'The requested page does not exist.');

    		}

    		return $this->_model;

    	}



i try this:

url manage-


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

but its still not working becouse the model need id in url.

any idea please?

Configure routing to pass ‘profile/<username:(\w+)>’ (tune regexp to match your own needs) to ‘user/view’.

You can also use action param binding for convenience




public function actionView($username = null)

{

    $model = $this->loadModel($username);

...

}



Then modify your conditions to satisfy new requirements


public function loadModel($username = null) 

{

 ...

    if ($username) {

        $this->_model=User::model()->find('username = :u', array(':u' => $username));

    }

}

And don’t EVER use short “if” syntax. Braces are mandatory, that’s the standard.

Thanx for the reply

But now i see no data from user, i think it’s still not passing the data from a user like it chulde be

That’s because you’re doing it wrong.

Paste your current code including routing rules and controller.

I was looking for a solution for the same problem and your answer, ORey, worked like charm. Many thanks!