How to show or hide a content for users

Hi guys!

I have this item in my site/index.php:

<?= Html::a( Yii::t('app', 'Utenti'), ['utenti/index'] ) ?>

I need to show the page only for the user with role 0 (not for 1 and 2).
What I have to do??
Thanks a lot for helping me!!!

Hi,

well, to show it dependant on roles, there is https://www.yiiframework.com/doc/api/2.0/yii-web-user#can()-detail
But you may want to restrict access at the location of that link to.

1 Like

I do this in my layout file for my Menus by doing something along the lines of

if(Yii::$app->user->can('TheNameOfTheRole')){
	echo Html::a( Yii::t('app', 'Utenti'), ['utenti/index'] ) ;
}
2 Likes

Now it’s ok. Thank you very much!