I am unable to redirect user in Yii after login











up vote
0
down vote

favorite












I am new to Yii so i decided to write a simple login script just to put to test what i have learnt so far.



For some reasons my code doesn't work as it should.



Ideally if username and password exist, it ought to redirect to the index view and also and if it doesn't exist it ought to redirect to the login view.



If username and password exist it redirects as it ought to and it also shows the right session value but if username and password doesn't exist i also get the same result.



Any idea what i am doing wrong?



Here is my controller code:



<?php

namespace appcontrollers;

use appmodelsUsers;
use Yii;

class UsersController extends yiiwebController
{
public function actionIndex()
{

$model = new users();
return $this->render('login', [
'model' => $model,
]);
}

public function actionLogin()
{
$model = new Users();
if ($model->load(Yii::$app->request->post())) {
// form inputs are valid, do something here
$request = Yii::$app->request;
$form_values = $request->post('Users');
//var_dump($form_values['email']); exit;
//echo $form_values['email'];
if($model !==NULL){
$model = $model->doLogin($form_values['email'],$form_values['password']);
$session = Yii::$app->session;
$session->setFlash('login', 'Login succesful.');
return $this->render('index', [
'model' => $model,
]);
}

elseif($model === NULL){

$session = Yii::$app->session;
$session->setFlash('login', 'Invalid Login.');
return $this->render('login', [
'model' => $model,
]);
}
}

}
}









share|improve this question




























    up vote
    0
    down vote

    favorite












    I am new to Yii so i decided to write a simple login script just to put to test what i have learnt so far.



    For some reasons my code doesn't work as it should.



    Ideally if username and password exist, it ought to redirect to the index view and also and if it doesn't exist it ought to redirect to the login view.



    If username and password exist it redirects as it ought to and it also shows the right session value but if username and password doesn't exist i also get the same result.



    Any idea what i am doing wrong?



    Here is my controller code:



    <?php

    namespace appcontrollers;

    use appmodelsUsers;
    use Yii;

    class UsersController extends yiiwebController
    {
    public function actionIndex()
    {

    $model = new users();
    return $this->render('login', [
    'model' => $model,
    ]);
    }

    public function actionLogin()
    {
    $model = new Users();
    if ($model->load(Yii::$app->request->post())) {
    // form inputs are valid, do something here
    $request = Yii::$app->request;
    $form_values = $request->post('Users');
    //var_dump($form_values['email']); exit;
    //echo $form_values['email'];
    if($model !==NULL){
    $model = $model->doLogin($form_values['email'],$form_values['password']);
    $session = Yii::$app->session;
    $session->setFlash('login', 'Login succesful.');
    return $this->render('index', [
    'model' => $model,
    ]);
    }

    elseif($model === NULL){

    $session = Yii::$app->session;
    $session->setFlash('login', 'Invalid Login.');
    return $this->render('login', [
    'model' => $model,
    ]);
    }
    }

    }
    }









    share|improve this question


























      up vote
      0
      down vote

      favorite









      up vote
      0
      down vote

      favorite











      I am new to Yii so i decided to write a simple login script just to put to test what i have learnt so far.



      For some reasons my code doesn't work as it should.



      Ideally if username and password exist, it ought to redirect to the index view and also and if it doesn't exist it ought to redirect to the login view.



      If username and password exist it redirects as it ought to and it also shows the right session value but if username and password doesn't exist i also get the same result.



      Any idea what i am doing wrong?



      Here is my controller code:



      <?php

      namespace appcontrollers;

      use appmodelsUsers;
      use Yii;

      class UsersController extends yiiwebController
      {
      public function actionIndex()
      {

      $model = new users();
      return $this->render('login', [
      'model' => $model,
      ]);
      }

      public function actionLogin()
      {
      $model = new Users();
      if ($model->load(Yii::$app->request->post())) {
      // form inputs are valid, do something here
      $request = Yii::$app->request;
      $form_values = $request->post('Users');
      //var_dump($form_values['email']); exit;
      //echo $form_values['email'];
      if($model !==NULL){
      $model = $model->doLogin($form_values['email'],$form_values['password']);
      $session = Yii::$app->session;
      $session->setFlash('login', 'Login succesful.');
      return $this->render('index', [
      'model' => $model,
      ]);
      }

      elseif($model === NULL){

      $session = Yii::$app->session;
      $session->setFlash('login', 'Invalid Login.');
      return $this->render('login', [
      'model' => $model,
      ]);
      }
      }

      }
      }









      share|improve this question















      I am new to Yii so i decided to write a simple login script just to put to test what i have learnt so far.



      For some reasons my code doesn't work as it should.



      Ideally if username and password exist, it ought to redirect to the index view and also and if it doesn't exist it ought to redirect to the login view.



      If username and password exist it redirects as it ought to and it also shows the right session value but if username and password doesn't exist i also get the same result.



      Any idea what i am doing wrong?



      Here is my controller code:



      <?php

      namespace appcontrollers;

      use appmodelsUsers;
      use Yii;

      class UsersController extends yiiwebController
      {
      public function actionIndex()
      {

      $model = new users();
      return $this->render('login', [
      'model' => $model,
      ]);
      }

      public function actionLogin()
      {
      $model = new Users();
      if ($model->load(Yii::$app->request->post())) {
      // form inputs are valid, do something here
      $request = Yii::$app->request;
      $form_values = $request->post('Users');
      //var_dump($form_values['email']); exit;
      //echo $form_values['email'];
      if($model !==NULL){
      $model = $model->doLogin($form_values['email'],$form_values['password']);
      $session = Yii::$app->session;
      $session->setFlash('login', 'Login succesful.');
      return $this->render('index', [
      'model' => $model,
      ]);
      }

      elseif($model === NULL){

      $session = Yii::$app->session;
      $session->setFlash('login', 'Invalid Login.');
      return $this->render('login', [
      'model' => $model,
      ]);
      }
      }

      }
      }






      php yii yii2






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 4 at 10:38









      rob006

      8,3213930




      8,3213930










      asked Nov 4 at 9:47









      Chi

      34




      34
























          1 Answer
          1






          active

          oldest

          votes

















          up vote
          1
          down vote



          accepted










          This is because your condition does not make sense and your else branch is a dead code.



          First you're initializing $model variable:



          $model = new Users();


          Then your testing if it is not null:



          if($model !==NULL){


          Which will always return true since you already initialized this variable to contain Users model, there is no chance it will be null. You should probably have something like this:



          class UsersController extends yiiwebController {

          public function actionIndex() {
          $model = Users::findOne(Yii::$app->user->id);
          return $this->render('index', [
          'model' => $model,
          ]);
          }

          public function actionLogin() {
          $model = new Users();
          if ($model->load(Yii::$app->request->post()) && $model->login()) {
          Yii::$app->session->setFlash('login', 'Login successful.');
          return $this->redirect(['index']);
          }

          return $this->render('login', [
          'model' => $model,
          ]);
          }
          }


          And handle login (validating username and password) in Users::login() method. See example in basic application template.






          share|improve this answer





















          • Okay @rob006 please how is my else branch a dead code? i need to have an because i need to let the user know if login is not successfull.
            – Chi
            2 days ago










          • @Chi your else is a dead code because if the $model===NULL then it would never pass the $model->load() and will throw an exception, and hence it is never executed
            – Muhammad Omer Aslam
            2 days ago












          • Thanks @MuhammadOmerAslam i made an update to my action Login, this is what i currently have:
            – Chi
            2 days ago






          • 1




            @Chi you can use this answer posted by roob006 it should work correctly in your case
            – Muhammad Omer Aslam
            2 days ago












          • @MuhammadOmerAslam i might be wrong but i cant see anywhere he addressed the case if user does not exist. If user does not exist it should also give an error.
            – Chi
            2 days ago











          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














           

          draft saved


          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53139504%2fi-am-unable-to-redirect-user-in-yii-after-login%23new-answer', 'question_page');
          }
          );

          Post as a guest
































          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes








          up vote
          1
          down vote



          accepted










          This is because your condition does not make sense and your else branch is a dead code.



          First you're initializing $model variable:



          $model = new Users();


          Then your testing if it is not null:



          if($model !==NULL){


          Which will always return true since you already initialized this variable to contain Users model, there is no chance it will be null. You should probably have something like this:



          class UsersController extends yiiwebController {

          public function actionIndex() {
          $model = Users::findOne(Yii::$app->user->id);
          return $this->render('index', [
          'model' => $model,
          ]);
          }

          public function actionLogin() {
          $model = new Users();
          if ($model->load(Yii::$app->request->post()) && $model->login()) {
          Yii::$app->session->setFlash('login', 'Login successful.');
          return $this->redirect(['index']);
          }

          return $this->render('login', [
          'model' => $model,
          ]);
          }
          }


          And handle login (validating username and password) in Users::login() method. See example in basic application template.






          share|improve this answer





















          • Okay @rob006 please how is my else branch a dead code? i need to have an because i need to let the user know if login is not successfull.
            – Chi
            2 days ago










          • @Chi your else is a dead code because if the $model===NULL then it would never pass the $model->load() and will throw an exception, and hence it is never executed
            – Muhammad Omer Aslam
            2 days ago












          • Thanks @MuhammadOmerAslam i made an update to my action Login, this is what i currently have:
            – Chi
            2 days ago






          • 1




            @Chi you can use this answer posted by roob006 it should work correctly in your case
            – Muhammad Omer Aslam
            2 days ago












          • @MuhammadOmerAslam i might be wrong but i cant see anywhere he addressed the case if user does not exist. If user does not exist it should also give an error.
            – Chi
            2 days ago















          up vote
          1
          down vote



          accepted










          This is because your condition does not make sense and your else branch is a dead code.



          First you're initializing $model variable:



          $model = new Users();


          Then your testing if it is not null:



          if($model !==NULL){


          Which will always return true since you already initialized this variable to contain Users model, there is no chance it will be null. You should probably have something like this:



          class UsersController extends yiiwebController {

          public function actionIndex() {
          $model = Users::findOne(Yii::$app->user->id);
          return $this->render('index', [
          'model' => $model,
          ]);
          }

          public function actionLogin() {
          $model = new Users();
          if ($model->load(Yii::$app->request->post()) && $model->login()) {
          Yii::$app->session->setFlash('login', 'Login successful.');
          return $this->redirect(['index']);
          }

          return $this->render('login', [
          'model' => $model,
          ]);
          }
          }


          And handle login (validating username and password) in Users::login() method. See example in basic application template.






          share|improve this answer





















          • Okay @rob006 please how is my else branch a dead code? i need to have an because i need to let the user know if login is not successfull.
            – Chi
            2 days ago










          • @Chi your else is a dead code because if the $model===NULL then it would never pass the $model->load() and will throw an exception, and hence it is never executed
            – Muhammad Omer Aslam
            2 days ago












          • Thanks @MuhammadOmerAslam i made an update to my action Login, this is what i currently have:
            – Chi
            2 days ago






          • 1




            @Chi you can use this answer posted by roob006 it should work correctly in your case
            – Muhammad Omer Aslam
            2 days ago












          • @MuhammadOmerAslam i might be wrong but i cant see anywhere he addressed the case if user does not exist. If user does not exist it should also give an error.
            – Chi
            2 days ago













          up vote
          1
          down vote



          accepted







          up vote
          1
          down vote



          accepted






          This is because your condition does not make sense and your else branch is a dead code.



          First you're initializing $model variable:



          $model = new Users();


          Then your testing if it is not null:



          if($model !==NULL){


          Which will always return true since you already initialized this variable to contain Users model, there is no chance it will be null. You should probably have something like this:



          class UsersController extends yiiwebController {

          public function actionIndex() {
          $model = Users::findOne(Yii::$app->user->id);
          return $this->render('index', [
          'model' => $model,
          ]);
          }

          public function actionLogin() {
          $model = new Users();
          if ($model->load(Yii::$app->request->post()) && $model->login()) {
          Yii::$app->session->setFlash('login', 'Login successful.');
          return $this->redirect(['index']);
          }

          return $this->render('login', [
          'model' => $model,
          ]);
          }
          }


          And handle login (validating username and password) in Users::login() method. See example in basic application template.






          share|improve this answer












          This is because your condition does not make sense and your else branch is a dead code.



          First you're initializing $model variable:



          $model = new Users();


          Then your testing if it is not null:



          if($model !==NULL){


          Which will always return true since you already initialized this variable to contain Users model, there is no chance it will be null. You should probably have something like this:



          class UsersController extends yiiwebController {

          public function actionIndex() {
          $model = Users::findOne(Yii::$app->user->id);
          return $this->render('index', [
          'model' => $model,
          ]);
          }

          public function actionLogin() {
          $model = new Users();
          if ($model->load(Yii::$app->request->post()) && $model->login()) {
          Yii::$app->session->setFlash('login', 'Login successful.');
          return $this->redirect(['index']);
          }

          return $this->render('login', [
          'model' => $model,
          ]);
          }
          }


          And handle login (validating username and password) in Users::login() method. See example in basic application template.







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 4 at 10:38









          rob006

          8,3213930




          8,3213930












          • Okay @rob006 please how is my else branch a dead code? i need to have an because i need to let the user know if login is not successfull.
            – Chi
            2 days ago










          • @Chi your else is a dead code because if the $model===NULL then it would never pass the $model->load() and will throw an exception, and hence it is never executed
            – Muhammad Omer Aslam
            2 days ago












          • Thanks @MuhammadOmerAslam i made an update to my action Login, this is what i currently have:
            – Chi
            2 days ago






          • 1




            @Chi you can use this answer posted by roob006 it should work correctly in your case
            – Muhammad Omer Aslam
            2 days ago












          • @MuhammadOmerAslam i might be wrong but i cant see anywhere he addressed the case if user does not exist. If user does not exist it should also give an error.
            – Chi
            2 days ago


















          • Okay @rob006 please how is my else branch a dead code? i need to have an because i need to let the user know if login is not successfull.
            – Chi
            2 days ago










          • @Chi your else is a dead code because if the $model===NULL then it would never pass the $model->load() and will throw an exception, and hence it is never executed
            – Muhammad Omer Aslam
            2 days ago












          • Thanks @MuhammadOmerAslam i made an update to my action Login, this is what i currently have:
            – Chi
            2 days ago






          • 1




            @Chi you can use this answer posted by roob006 it should work correctly in your case
            – Muhammad Omer Aslam
            2 days ago












          • @MuhammadOmerAslam i might be wrong but i cant see anywhere he addressed the case if user does not exist. If user does not exist it should also give an error.
            – Chi
            2 days ago
















          Okay @rob006 please how is my else branch a dead code? i need to have an because i need to let the user know if login is not successfull.
          – Chi
          2 days ago




          Okay @rob006 please how is my else branch a dead code? i need to have an because i need to let the user know if login is not successfull.
          – Chi
          2 days ago












          @Chi your else is a dead code because if the $model===NULL then it would never pass the $model->load() and will throw an exception, and hence it is never executed
          – Muhammad Omer Aslam
          2 days ago






          @Chi your else is a dead code because if the $model===NULL then it would never pass the $model->load() and will throw an exception, and hence it is never executed
          – Muhammad Omer Aslam
          2 days ago














          Thanks @MuhammadOmerAslam i made an update to my action Login, this is what i currently have:
          – Chi
          2 days ago




          Thanks @MuhammadOmerAslam i made an update to my action Login, this is what i currently have:
          – Chi
          2 days ago




          1




          1




          @Chi you can use this answer posted by roob006 it should work correctly in your case
          – Muhammad Omer Aslam
          2 days ago






          @Chi you can use this answer posted by roob006 it should work correctly in your case
          – Muhammad Omer Aslam
          2 days ago














          @MuhammadOmerAslam i might be wrong but i cant see anywhere he addressed the case if user does not exist. If user does not exist it should also give an error.
          – Chi
          2 days ago




          @MuhammadOmerAslam i might be wrong but i cant see anywhere he addressed the case if user does not exist. If user does not exist it should also give an error.
          – Chi
          2 days ago


















           

          draft saved


          draft discarded



















































           


          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53139504%2fi-am-unable-to-redirect-user-in-yii-after-login%23new-answer', 'question_page');
          }
          );

          Post as a guest




















































































          這個網誌中的熱門文章

          Tangent Lines Diagram Along Smooth Curve

          Yusuf al-Mu'taman ibn Hud

          Zucchini