I want to use a method to check some security settings for my user. When they do not have access, I want to send them to site/error but no matter what I do, I can’t get it to work. I have read the documentation for both Yii1 and Yii2 and I am very confused about how ErrorAction works. My config already contains the following code:
'errorHandler' => [
'errorAction' => 'site/error',
],
My controller looks something like this:
<?php
namespace app\models;
use Yii;
use yii\base\Model;
use yii\base\ErrorHandler;
class Security extends Model{
/* I added this method as per documentation */
public function actions()
{
return [
'error' => [
'class' => 'yii\web\ErrorAction',
],
];
}
/* Here is my custom method to check security */
static function auth(){
if(Yii::$app->user->isGuest)
{
$message = "Access denied.";
/* This next line shows me the error in my view, but does not redirect me to site/error
with the message, which is what I want.
*/
Yii::$app->getSession()->setFlash('error', $message);
}
echo Yii::$app->view->renderFile('site/error', $message); //tried this, but no luck
}
protected function actionError(){
//Is this method even needed?
}
}
in my view file, I have:
use app\models\Security;
Security::auth();