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,
]);
}
}
}
}
php yii yii2
add a comment |
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,
]);
}
}
}
}
php yii yii2
add a comment |
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,
]);
}
}
}
}
php yii yii2
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
php yii yii2
edited Nov 4 at 10:38
rob006
8,3213930
8,3213930
asked Nov 4 at 9:47
Chi
34
34
add a comment |
add a comment |
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.
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
|
show 2 more comments
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.
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
|
show 2 more comments
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.
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
|
show 2 more comments
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.
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.
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
|
show 2 more comments
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
|
show 2 more comments
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password