【问题标题】:How to call yii component useridentity class from controller如何从控制器调用 yii 组件用户身份类
【发布时间】:2014-03-17 17:10:59
【问题描述】:

我正在尝试使用 Yii 创建一个简单的登录 这是我的身份验证控制器

class AuthController  extends Controller
{
    /**
    * Declare class-based actions.
    */
    public function actionLogin()
    {
        $model = new LoginForm;
        $post = Yii::app()->request->getPost('LoginForm');
        // If form is submitted
        if($post) {
            $identity = new UserIdentity($post['username'], $post['password']);
            echo $identity->testing();
            if($identity->authenticate()) {
                echo 'yes';
            } else {
                echo 'no';
            }
            exit;
        }
        $this->render('login', array('model' => $model));   
    }
}

这是我的用户身份

class UserIdentity extends CUserIdentity
{


    private $_id;

    public function authenticate()
    {   echo 'testing';
        $user = LoginForm::model()->findByAttributes(array('username' => $this->username));
        if(is_null($user)) {
            %this->errorCode=self::ERROR_USERNAME_INVALID;
        } else if($user->password != $this->password) {
            $this->errorCode=self::ERROR_PASSWORD_INVALID;
        } else {
            $this->_id = $user->id;
            $this->errorCode=self::ERROR_NONE;
        }

        return !$this->errorCode;
    }

    function getId()
    {
        return $this->_id;
    }
}

我提到了 echo 'yes' 和 echo 'no' 但两者都没有显示。如何纠正它

【问题讨论】:

    标签: php yii yii-components


    【解决方案1】:

    首先,你甚至不会看到那些 echo 语句,最终用户在 Yii 中看到的唯一可视化的东西就是“视图”。对于我的登录代码,与您的登录代码略有不同,在确认身份验证后,我的应用程序被重定向到主页。您的自定义 UserIdentity 文件看起来不错,但同样,甚至不会看到该 echo 语句。此 UserIdentity 文件仅用于在后台执行自定义用户身份验证。

    在我的 UserController(而不是你的 AuthController)中,我的 actionLogin 是:

    public function actionLogin()
    {
        $model=new LoginForm;
    
        // if it is ajax validation request
        if(isset($_POST['ajax']) && $_POST['ajax']==='login-form')
        {
            echo CActiveForm::validate($model);
            Yii::app()->end();
        }
    
        // collect user input data
        if(isset($_POST['LoginForm']))
        {
            $model->attributes=$_POST['LoginForm'];
            // validate user input and redirect to the previous page if valid
            if($model->validate() && $model->login())               
            {
                $this->redirect(Yii::app()->user->returnUrl);
            }
        }
        $this->render('/user/login',array('model'=>$model));
    }
    

    例如,从上面的内容中,您可以重定向到您所在的上一页或重定向到“/site/index”处的主站点视图,并且在该页面下有一些代码可以执行一些任意功能或打印出 HTML,具体取决于如果您已登录或未登录。一个过于简单的站点视图示例:

    <?php
    /* @var $this SiteController */
    if (Yii::app()->user->isGuest)
    {
        // Do stuff here if user is guest.
        echo 'User is a guest.';
    }
    else
    {
        // Do stuff here if user is authenticated.
        echo 'User is authenticated.';
    }
    ?>
    

    【讨论】:

      猜你喜欢
      • 2018-10-19
      • 1970-01-01
      • 1970-01-01
      • 2021-05-02
      • 1970-01-01
      • 2016-05-25
      • 1970-01-01
      • 2013-11-28
      • 2016-10-06
      相关资源
      最近更新 更多