Hi, I wrote this simple code :
public static function setDefault($clientName)
{
$session = Yii::$app->session;
$url = Yii::$app->urlManager->createUrl('client/change');
$session->open();
if (isset($session->client_id)) {
if (($c = Client::find($session->client_id)) === null) {
Yii::$app->getResponse()->redirect($url);
}
else {
$session->client_id = $c->id;
$session->client = $c->name;
}
}
else {
Yii::$app->getResponse()->redirect($url);
}
}
This idea is :
if "client_id" variable is defined in session, check if the value existe in DB, if not => redirection to client/change
If "client_id" variable is not defined => redirection to client/change
My questions are :
-
Wich event use ? beforeAction ? afterRequest ?
-
Where put this code ? Controller ? Model ? somethings else ?
-
How to check the current request route (not redirect if I’m in client/change, to avoid loop)
Thanks you for your help