Page not found #404 for a custom login page


(shingai) #1

hi

i have create a page, i link the page on my navigation bar. when i click the link the it displays page not found. i duplicated the code for the login system but for a different account but it seems not to find this particular page , but the original login page works fine


#2

hi,
probably you did some mistake in method signature.
Could you post some code so we can help you in a better way?
m


(shingai) #3
<?php
namespace common\models;

use Yii;
use yii\base\Model;

/**
 * Login form
 */
class DocLoginForm extends Model
{
    public $username;
    public $password;
    public $rememberMe = true;

    private $_doctor;

    /**
     * {@inheritdoc}
     */
    public function rules()
    {
        return [
            // username and password are both required
            [['username', 'password'], 'required'],
            // rememberMe must be a boolean value
            ['rememberMe', 'boolean'],
            // password is validated by validatePassword()
            ['password', 'validatePassword'],
        ];
    }

    /**
     * Validates the password.
     * This method serves as the inline validation for password.

(shingai) #4
<?php
namespace common\models;

use Yii;
use yii\base\Model;

/**
 * Login form
 */
class DLoginForm extends Model
{
    public $username;
    public $password;
    public $rememberMe = true;

    private $_doctor;

    /**
     * {@inheritdoc}
     */
    public function rules()
    {
        return [
            // username and password are both required
            [['username', 'password'], 'required'],
            // rememberMe must be a boolean value
            ['rememberMe', 'boolean'],
            // password is validated by validatePassword()
            ['password', 'validatePassword'],
        ];
    }

    /**
     * Validates the password.
     * This method serves as the inline validation for password.

#5

You better use code tag.
Anyway you cannot call model method by link.
It’s just for action controller method.


(shingai) #6

My model class

namespace common\models;

use Yii;
use yii\base\Model;

/**
 * Login form
 */
class DLoginForm extends Model
{
    public $username;
    public $password;
    public $rememberMe = true;

    private $_doctor;

    public function rules()
    {
        return [
            // username and password are both required
            [['username', 'password'], 'required'],
            // rememberMe must be a boolean value
            ['rememberMe', 'boolean'],
            // password is validated by validatePassword()
            ['password', 'validatePassword'],
        ];
    }

    /**
     * Validates the password.
     * This method serves as the inline validation for password.
     *
     * @param string $attribute the attribute currently being validated
     * @param array $params the additional name-value pairs given in the rule
     */
    public function validatePassword($attribute, $params)
    {
        if (!$this->hasErrors()) {
            $doctor = $this->getUser();
            if (!$doctor || !$doctor->validatePassword($this->password)) {
                $this->addError($attribute, 'Incorrect username or password.');
            }
        }
    }

    /**
     * Logs in a user using the provided username and password.
     *
     * @return bool whether the user is logged in successfully
     */
    public function login()
    {
        if ($this->validate()) {
            return Yii::$app->user->login($this->getUser(), $this->rememberMe ? 3600 * 24 * 30 : 0);
        }

        return false;
    }

    /**
     * Finds user by [[username]]
     *
     * @return |null
     */
    protected function getUser()
    {
        if ($this->_doctor === null) {
            $this->_doctor = Doctor::findByUsername($this->username);
        }

        return $this->_doctor;
    }
}

the controller method is below

  public function actionDocLogin()
          {
           if (!Yii::$app->user->isGuest) {
               return $this->goHome();
           }

        $model = new DloginForm();
                 if ($model->load(Yii::$app->request->post()) && $model->login()) {
                 return $this->goBack();
        } else {
                 $model->password = '';

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

in my main

       ['label' => 'Doctor Login', 'url' => ['/site/dlogin']],

could you assist me by showing me where i made the mistake


#7

Your action URL is:
[’/site/doc-login’]
And not
[’/site/dlogin’]


(shingai) #8

let me give that a try , thanx


(shingai) #9

when i login im getting an error , Exception (Unknown Property) ‘yii\base\UnknownPropertyException’ with message ‘Getting unknown property: common\models\DloginForm::auth_key’.


(Softark) #10

Hi @shingai,

I’ve added code tags to your previous posts. Please use them in your future posts, because they will make your code much easier to read.

``` (opening code tag)
use Yii;
public function actionDoctorLogin()
{
   ... bla bla
}
``` (closing code tag)

As you see in the example above, enclose your code block in a pair of lines of 3 backquotes(backticks).


(shingai) #11

thanx for the help


(Softark) #12

The error message says that your DloginForm doesn’t have a necessary attribute of auth_key, so you have to add it to DloginForm model.

But I’m not sure if it would work fine, because I feel you’ve got lost in earlier stage when you tried to make a custom login page for doctors.

Could you tell us your design about users? Do you want to manage users of different kinds, i.e., patients and doctors?


(shingai) #13

i managed to solve the error i was getting about the attribute auth_key . on the other hand i can now log in into my doctor account but there is this thing that is happening when i refresh , i get sent back to another user account which is not doctor, the account is from the user table .

about my design i trying to have three accounts that sign-in separately and have three different views, these three would be doctor , farm and customer.


(Softark) #14

That’s exactly what I was afraid of.
It is probably because a user shares the same ID of that doctor.

In your design, doctors are defined in doctor table. And the ID of a doctor is unique among doctors. But some farm or customer may have the same ID, because they are defined in another table.