php - Username instead of id in URL - yii-user -
I am using "yii-user" (module / user), now user url is mysite. Com / user / user / view / id / 1
and want to change me to mysite.com/profile/username
User Controller:
< Pre> View Public Function Action () {$ Model = $ this- & gt; LoadModel (); $ This- & gt; Render ('view', array ('model' =>; $ models,)); } Public function loads () {if ($ this-> _model === faucet) {if (isset ($ _ GET ['id'])) $ this- & gt; _model = User :: model () - & gt; FindbyPk ($ _ [['ID']); If ($ this-> _model === faucet) Throw new CHttpException (404, 'The requested page does not exist.'); } $ $ -> Modell; } / ** * returns the data model based on the primary key given in GIT variable. * If a data model is not found, an HTTP exception will be raised. * @ PRM Integer Primary Key Value 'id' GET variable * / Public function loadUser ($ id = null) {if ($ this-> _model === faucet) {if ($ id! == null || isset $ _GET ['id'])) $ the- & gt; _model = User :: model () - & gt; Find Pk ($ id! == zero? $ Id: $ _GET ['id']); If ($ this-> _model === faucet) Throw new CHttpException (404, 'The requested page does not exist.'); } $ $ -> Modell; }
I try to add this URL rule 'profile / & lt; Username & gt; = & Gt; 'User / User / View',
but it still does not work because the ID of the model URL is required.
Any thoughts please?
You should only modify your operation to handle the username, such as ($ Id = zero, $ Username = null] {if (isset ($ id)) $ model = user :: model () - & gt; FindByPk ($ id)
); Else if isset ($ username)) $ model = user :: model () - & gt; FindByAttributes (array ('username' = & gt; $ username)); If ($ Model === Faucet) Throw New CHttpException (404, 'The requested page does not exist.'); $ This- & gt; Presenting ('view', array ('model' =>; $ models,)); }
And make sure:
- The username is unique,
- You have the corresponding URL rule in your config.
Comments
Post a Comment