Yii : Facebook style url route

2019-07-30 14:55发布

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条回答
【Aperson】
2楼-- · 2019-07-30 15:24

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 .

查看更多
登录 后发表回答