Yii2 Redirection
There is a model for the form in it, from one method it is necessary to redirect the user to another page. I do this:
...Модель...
public function test(){
return Yii::$app->response->redirect(['notactivated']);
}
...
But for some reason there is a transition to the main index page instead of the page notactivated.php.
Tried again:
...Модель...
public function test(){
return Yii::$app->response->redirect(['site/notactivated']);
}
...
And the same situation.
What could be the reason?
_____ ADDED _____ _
Controller code:
class SiteController extends Controller
{
public function actionNotactivated()
{
return $this->render('notactivated');
}
}
Model code
class LoginForm extends Model
{
public function login()
{
$u = Users::find()->where(['name' => $this->username])->one();
if ($u->activated == 0){
return Yii::$app->response->redirect(['site/notactivated']); // вот тут я пытался сделать редирект
//return Yii::$app->response->redirect(Url::to(['site/about']));
//return $this->redirect(Url::to(['site/about']));
}
if ($this->validate()) {
return Yii::$app->user->login($this->getUser(), $this->rememberMe ? 3600*24*30 : 0);
}
return false;
}
}
0
1 answers
public function actionTest() {
$model = new Model();
if ($model->test()) {
Yii::$app->response->redirect(Url::to('site/notactivated'));
}
}
Updated
public function actionLogin() {
$model = new LoginForm();
if ($model->load(Yii::$app->request->post()) && !$model->login()) {
return $this->render('notactivated');
}
return $this->render('login');
}
And in the model
class LoginForm extends Model {
public function login() {
if (!$this->validate()) {
return false;
}
$u = Users::find()->where(['name' => $this->username])->one();
if ($u->activated == 0) {
return false;
}
return Yii::$app->user->login($this->getUser(), $this->rememberMe ? 3600 * 24 * 30 : 0);
}
}
2
Author: Ninazu, 2016-09-07 10:50:55