I'm aware there's a similar post to this but just to confirm my understanding.
I just start using Yii2 PHP. I've used dektrium/yii2-user that can login and register. I want to do a beforeAction() to check logged user auth.key if exist in database (XAMPP MYSQL). Below is the code I want to performed on.
//Action direct to json.php.
public function actionJson()
{
return $this->render('json');
}
If the statement true will direct to the page, else shows a alert error.
I know the exact code is
public function beforeAction($action){}
What I'm confused on is where do I actually put beforeAction() at.
BeforeAction()
This method is invoked right before an action is executed.
https://www.yiiframework.com/doc/api/2.0/yii-base-controller#beforeAction()-detail
You must place it inside your login controller (LoginController.php) class. Remember to call the parent inside the function as :
public function beforeAction($action)
{
if (!parent::beforeAction($action)) {
return false;
}
return true; // or false to not run the action
}
Then all the actions from that controller will use your custom beforeAction function.