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.
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.