Yii : Facebook style url route

孤街醉人 提交于 2019-12-11 05:44:38

问题


I would like to set up my Yii webapp so that the url

/username will show the user record for that user.

I've tried

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

, but this results in an error 400.

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

works.


回答1:


I've managed to work out what I was doing wrong.

The variables that you put in your url routes need to correspond to the functions in your controller actions.

So I changed the standard actionView generated by gii to

    public function actionView($username) {
         $model=User::model()->find("username = '".$username."'");
     $this->render('view',array('model'=>$model);
     }

Now . . . '<username>'=> 'user/view' as a url route works as expected .



来源:https://stackoverflow.com/questions/11369656/yii-facebook-style-url-route

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!