Virtual controller/action

Hello there! I’m new to Yii and before I start coding something I would like to know if it’s already there or not.

I’m trying to create an application where users should be able to access other user’s profile by entering a URL in the format www.website.com/username. As you can see I would like to bypass the existing format of www.website.com/controller/action/params, with the existing format I can easily implement something like www.website.com/profile/show/username but I would like to shorter that URL to the other format.

Is there any functionality in Yii that allows to define a “virtual” controller and action where a URL in the format www.website.com/username gets routed as www.website.com/virtualcontroller/virtualaction/username when it finds that there’s no controller with the name “username”?

Thanks for any help.

take a look at the guide : url rewrite :lol:

Dear Friend

Try to go to the route user/view,by following format.

For example, in views/user/_view.php, the following can be the link to user/view.




<?php echo CHtml::link(CHtml::encode($data->name), array('view', 'view'=>$data->username)); ?>



instead of




<?php echo CHtml::link(CHtml::encode($data->id), array('view', 'id'=>$data->id)); ?>



Now change the actionView in UserController.php




public function actionView($view)

	{

		$model=User::model()->findByAttributes(array('username'=>$view));

		

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

			

			'model'=>$model,

		));

	}



Now make the changes in urlManager.




'urlManager'=>array(

			'urlFormat'=>'path',

			'showScriptName'=>false,//if it works in your server...

			'rules'=>array(

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

				

			),

		),



You have to modify any url going to route user/view as mentioned above and username should be unique.

Thanks both for the answers… I checked both but not sure I can do what I’m trying to do. Both solutions seem to be a rewrite of the URL (unless I misunderstood what I read) or a way to provide users with links for other user profiles. The question is I want users to be able to send a link to someone for example a la Facebook where you can directly reach someones profile by accessing facebook.com/profile instead of facebook.com/user/view/profile.

I’m digging into the code and it seems I need to change the behavior of createController() inside CWebApplication that instead of returning null if a specified controller doesn’t exist to load and direct to another default/virtual controller. My guess is that I have to make the changes there as is in that method where the check is done to see if the controller specified exists or not. Of course then the default/virtual controller will check if the user specified exists or not.

CUrlManager seems to be key here also as that class returns the URL values and if I internally change the values then I guess I also need to change inside that object for the entire transaction.