How to make a 403 error?

Hello friends programmers!
Could you help me) the Idea is this:
On my website there is registration, login. Accordingly, there is a personal account of the user. How to do the following: if the user is logged in or not registered could not go to the personal Cabinet ("ishop.loc/cabinet.html") that a 403 error
HERE IT IS:
5cf7c4eb1175a773072437.jpeg
March 23rd 20 at 19:13
2 answers
March 23rd 20 at 19:15
Solution
for yii 2.
just for yii - I don't know. I'll find something'll update the answer.
use yii\web\HttpException;
//some code...
throw new HttpException(403/*or any code*/, 'Forbidden'/*or any message*/);

in General, you need to do so:
try {
 //any code
} catch (Exception $e) {
 echo 'exception Thrown: ', $e->getMessage(), "\n"; // any action/message
}
Hello, made this way (works):
if (Yii::$app->user->isGuest){
 throw new HttpException(403/*or any code*/, 'ERROR 403!!!!!!!'/*or any message*/);
}else{
 return $this->render('index');
 }

Whether made(shit code) ? Otherwise still works))) - Aletha_Boyle commented on March 23rd 20 at 19:18
@Aletha_Boyle, first, comments can remove. I left them for you to make it clearer.
second, it is better to use this design:
if (Yii::$app->user->isGuest === true){
 throw new HttpException(403, 'Forbidden');
return $this->render('index');

fundamentally nothing has changed, but the eye is nicer to read such code.
thirdly, messages should be informative and brief.
dry "you do not have enough rights," "prohibited" or "forbidden" it will come down. and looks nice, and informative.
and if the case, if it works - so it'll do. - isabelle commented on March 23rd 20 at 19:21
@isabelle, Thanks Ruslan) - Aletha_Boyle commented on March 23rd 20 at 19:24
@Aletha_Boyle, please, Ruslan. - isabelle commented on March 23rd 20 at 19:27
March 23rd 20 at 19:17
throw new yii\web\HttpException(403, 'Access denied');

Find more questions by tags Yii