How can I hide user_id from URL using yii framework?

安稳与你 提交于 2019-12-06 14:32:34

If each user has a unique username, you can describe a route that doesn't show their user id by putting this custom routing rule in config/main:

'urlManager' => array(
    'rules' => array(
        'user/<username:\S+>' => 'users/view',
    )
)

The \S+ should accept "@". Then pass that username to controllers/UsersController like so:

public function actionView($username) {...}

Instead of www.mydomain.com/user/update/username I would use www.mydomain.com/user/<username>/update and put this in the above urlManager rules array:

'user/<username:\S+>/update' => 'users/update',

and this in UsersController:

public function actionUpdate($username) {...}

You can also generate a unique, random number for each user before creation and route based on that number.

I think it's better to encrypt your id before passing it. And decrypt it when you reach there. There are many ways of generating such random/encrpted ids. For more security you should not give name like 'user', that you did. Try to modify that even. Hope you got the point...:)

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