Привет, я довольно новичок в инфраструктуре yii, в настоящее время пытаюсь установить логин через аутентификацию базы данных. но im неоднократно получая эту ошибку
CException
- Yii CDbConnection не удалось открыть соединение с БД: не удалось найти драйвер с облаком Google sql
- Как зашифровать пароль перед отправкой из формы входа?
- Приложение Yii как сделать скрытое поле для текущей даты даты в режиме создания?
- Yii CMultiFileUpload выбрать несколько файлов
- Правильный пароль не принимается в Yii login
CActiveForm и его поведение не имеют метода или закрытия с именем «getErrors».
может кто-нибудь помочь мне, если это
Хеш-контроллер
<?php class SiteController extends Controller { public function actions() { return array( 'captcha'=>array( 'class'=>'CCaptchaAction', 'backColor'=>0xFFFFFF, ), 'page'=>array( 'class'=>'CViewAction', ), ); } public function actionIndex() { $this->render('index'); } public function actionError() { if($error=Yii::app()->errorHandler->error) { if(Yii::app()->request->isAjaxRequest) echo $error['message']; else $this->render('error', $error); } } public function actionContact() { $model=new ContactForm; if(isset($_POST['ContactForm'])) { $model->attributes=$_POST['ContactForm']; if($model->validate()) { $name='=?UTF-8?B?'.base64_encode($model->name).'?='; $subject='=?UTF-8?B?'.base64_encode($model->subject).'?='; $headers="From: $name <{$model->email}>\r\n". "Reply-To: {$model->email}\r\n". "MIME-Version: 1.0\r\n". "Content-Type: text/plain; charset=UTF-8"; mail(Yii::app()->params['adminEmail'],$subject,$model->body,$headers); Yii::app()->user->setFlash('contact','Thank you for contacting us. We will respond to you as soon as possible.'); $this->refresh(); } } $this->render('contact',array('model'=>$model)); } public function actionLogin() { $form=new LoginForm; if(isset($_POST['LoginForm'])) { $form->attributes=$_POST['LoginForm']; if($form->validate() && $form->login()) $this->redirect(Yii::app()->user->returnUrl); } $this->render('login',array('form'=>$form)); } public function actionLogout() { Yii::app()->user->logout(); $this->redirect(Yii::app()->homeUrl); }
}
herers модель
<?php class LoginForm extends CFormModel { public $email; public $password; private $_identity; public function rules() { return array( array('email, password', 'required'), array('email', 'email'), array('password', 'authenticate'), ); } public function attributeLabels() { return array('email'=>'Email Address'); } public function authenticate($attribute,$params) { if(!$this->hasErrors()) // we only want to authenticate when no input errors { $identity=new UserIdentity($this->email,$this->password); $identity->authenticate(); switch($identity->errorCode) { case UserIdentity::ERROR_NONE: Yii::app()->user->login($identity); break; case UserIdentity::ERROR_USERNAME_INVALID: $this->addError('email','Email address is incorrect.'); break; default: // UserIdentity::ERROR_PASSWORD_INVALID $this->addError('password','Password is incorrect.'); break; } } } public function login() { if($this->_identity===null) { $this->_identity=new UserIdentity($this->username,$this->password); $this->_identity->authenticate(); } if($this->_identity->errorCode===UserIdentity::ERROR_NONE) { $duration=$this->rememberMe ? 3600*24*30 : 0; // 30 days Yii::app()->user->login($this->_identity,$duration); return true; } else return false; }
использования<?php class LoginForm extends CFormModel { public $email; public $password; private $_identity; public function rules() { return array( array('email, password', 'required'), array('email', 'email'), array('password', 'authenticate'), ); } public function attributeLabels() { return array('email'=>'Email Address'); } public function authenticate($attribute,$params) { if(!$this->hasErrors()) // we only want to authenticate when no input errors { $identity=new UserIdentity($this->email,$this->password); $identity->authenticate(); switch($identity->errorCode) { case UserIdentity::ERROR_NONE: Yii::app()->user->login($identity); break; case UserIdentity::ERROR_USERNAME_INVALID: $this->addError('email','Email address is incorrect.'); break; default: // UserIdentity::ERROR_PASSWORD_INVALID $this->addError('password','Password is incorrect.'); break; } } } public function login() { if($this->_identity===null) { $this->_identity=new UserIdentity($this->username,$this->password); $this->_identity->authenticate(); } if($this->_identity->errorCode===UserIdentity::ERROR_NONE) { $duration=$this->rememberMe ? 3600*24*30 : 0; // 30 days Yii::app()->user->login($this->_identity,$duration); return true; } else return false; }
данных<?php class LoginForm extends CFormModel { public $email; public $password; private $_identity; public function rules() { return array( array('email, password', 'required'), array('email', 'email'), array('password', 'authenticate'), ); } public function attributeLabels() { return array('email'=>'Email Address'); } public function authenticate($attribute,$params) { if(!$this->hasErrors()) // we only want to authenticate when no input errors { $identity=new UserIdentity($this->email,$this->password); $identity->authenticate(); switch($identity->errorCode) { case UserIdentity::ERROR_NONE: Yii::app()->user->login($identity); break; case UserIdentity::ERROR_USERNAME_INVALID: $this->addError('email','Email address is incorrect.'); break; default: // UserIdentity::ERROR_PASSWORD_INVALID $this->addError('password','Password is incorrect.'); break; } } } public function login() { if($this->_identity===null) { $this->_identity=new UserIdentity($this->username,$this->password); $this->_identity->authenticate(); } if($this->_identity->errorCode===UserIdentity::ERROR_NONE) { $duration=$this->rememberMe ? 3600*24*30 : 0; // 30 days Yii::app()->user->login($this->_identity,$duration); return true; } else return false; }
использования<?php class LoginForm extends CFormModel { public $email; public $password; private $_identity; public function rules() { return array( array('email, password', 'required'), array('email', 'email'), array('password', 'authenticate'), ); } public function attributeLabels() { return array('email'=>'Email Address'); } public function authenticate($attribute,$params) { if(!$this->hasErrors()) // we only want to authenticate when no input errors { $identity=new UserIdentity($this->email,$this->password); $identity->authenticate(); switch($identity->errorCode) { case UserIdentity::ERROR_NONE: Yii::app()->user->login($identity); break; case UserIdentity::ERROR_USERNAME_INVALID: $this->addError('email','Email address is incorrect.'); break; default: // UserIdentity::ERROR_PASSWORD_INVALID $this->addError('password','Password is incorrect.'); break; } } } public function login() { if($this->_identity===null) { $this->_identity=new UserIdentity($this->username,$this->password); $this->_identity->authenticate(); } if($this->_identity->errorCode===UserIdentity::ERROR_NONE) { $duration=$this->rememberMe ? 3600*24*30 : 0; // 30 days Yii::app()->user->login($this->_identity,$duration); return true; } else return false; }
данных<?php class LoginForm extends CFormModel { public $email; public $password; private $_identity; public function rules() { return array( array('email, password', 'required'), array('email', 'email'), array('password', 'authenticate'), ); } public function attributeLabels() { return array('email'=>'Email Address'); } public function authenticate($attribute,$params) { if(!$this->hasErrors()) // we only want to authenticate when no input errors { $identity=new UserIdentity($this->email,$this->password); $identity->authenticate(); switch($identity->errorCode) { case UserIdentity::ERROR_NONE: Yii::app()->user->login($identity); break; case UserIdentity::ERROR_USERNAME_INVALID: $this->addError('email','Email address is incorrect.'); break; default: // UserIdentity::ERROR_PASSWORD_INVALID $this->addError('password','Password is incorrect.'); break; } } } public function login() { if($this->_identity===null) { $this->_identity=new UserIdentity($this->username,$this->password); $this->_identity->authenticate(); } if($this->_identity->errorCode===UserIdentity::ERROR_NONE) { $duration=$this->rememberMe ? 3600*24*30 : 0; // 30 days Yii::app()->user->login($this->_identity,$duration); return true; } else return false; }
использованием<?php class LoginForm extends CFormModel { public $email; public $password; private $_identity; public function rules() { return array( array('email, password', 'required'), array('email', 'email'), array('password', 'authenticate'), ); } public function attributeLabels() { return array('email'=>'Email Address'); } public function authenticate($attribute,$params) { if(!$this->hasErrors()) // we only want to authenticate when no input errors { $identity=new UserIdentity($this->email,$this->password); $identity->authenticate(); switch($identity->errorCode) { case UserIdentity::ERROR_NONE: Yii::app()->user->login($identity); break; case UserIdentity::ERROR_USERNAME_INVALID: $this->addError('email','Email address is incorrect.'); break; default: // UserIdentity::ERROR_PASSWORD_INVALID $this->addError('password','Password is incorrect.'); break; } } } public function login() { if($this->_identity===null) { $this->_identity=new UserIdentity($this->username,$this->password); $this->_identity->authenticate(); } if($this->_identity->errorCode===UserIdentity::ERROR_NONE) { $duration=$this->rememberMe ? 3600*24*30 : 0; // 30 days Yii::app()->user->login($this->_identity,$duration); return true; } else return false; }
данных<?php class LoginForm extends CFormModel { public $email; public $password; private $_identity; public function rules() { return array( array('email, password', 'required'), array('email', 'email'), array('password', 'authenticate'), ); } public function attributeLabels() { return array('email'=>'Email Address'); } public function authenticate($attribute,$params) { if(!$this->hasErrors()) // we only want to authenticate when no input errors { $identity=new UserIdentity($this->email,$this->password); $identity->authenticate(); switch($identity->errorCode) { case UserIdentity::ERROR_NONE: Yii::app()->user->login($identity); break; case UserIdentity::ERROR_USERNAME_INVALID: $this->addError('email','Email address is incorrect.'); break; default: // UserIdentity::ERROR_PASSWORD_INVALID $this->addError('password','Password is incorrect.'); break; } } } public function login() { if($this->_identity===null) { $this->_identity=new UserIdentity($this->username,$this->password); $this->_identity->authenticate(); } if($this->_identity->errorCode===UserIdentity::ERROR_NONE) { $duration=$this->rememberMe ? 3600*24*30 : 0; // 30 days Yii::app()->user->login($this->_identity,$duration); return true; } else return false; }
использованием<?php class LoginForm extends CFormModel { public $email; public $password; private $_identity; public function rules() { return array( array('email, password', 'required'), array('email', 'email'), array('password', 'authenticate'), ); } public function attributeLabels() { return array('email'=>'Email Address'); } public function authenticate($attribute,$params) { if(!$this->hasErrors()) // we only want to authenticate when no input errors { $identity=new UserIdentity($this->email,$this->password); $identity->authenticate(); switch($identity->errorCode) { case UserIdentity::ERROR_NONE: Yii::app()->user->login($identity); break; case UserIdentity::ERROR_USERNAME_INVALID: $this->addError('email','Email address is incorrect.'); break; default: // UserIdentity::ERROR_PASSWORD_INVALID $this->addError('password','Password is incorrect.'); break; } } } public function login() { if($this->_identity===null) { $this->_identity=new UserIdentity($this->username,$this->password); $this->_identity->authenticate(); } if($this->_identity->errorCode===UserIdentity::ERROR_NONE) { $duration=$this->rememberMe ? 3600*24*30 : 0; // 30 days Yii::app()->user->login($this->_identity,$duration); return true; } else return false; }
данных<?php class LoginForm extends CFormModel { public $email; public $password; private $_identity; public function rules() { return array( array('email, password', 'required'), array('email', 'email'), array('password', 'authenticate'), ); } public function attributeLabels() { return array('email'=>'Email Address'); } public function authenticate($attribute,$params) { if(!$this->hasErrors()) // we only want to authenticate when no input errors { $identity=new UserIdentity($this->email,$this->password); $identity->authenticate(); switch($identity->errorCode) { case UserIdentity::ERROR_NONE: Yii::app()->user->login($identity); break; case UserIdentity::ERROR_USERNAME_INVALID: $this->addError('email','Email address is incorrect.'); break; default: // UserIdentity::ERROR_PASSWORD_INVALID $this->addError('password','Password is incorrect.'); break; } } } public function login() { if($this->_identity===null) { $this->_identity=new UserIdentity($this->username,$this->password); $this->_identity->authenticate(); } if($this->_identity->errorCode===UserIdentity::ERROR_NONE) { $duration=$this->rememberMe ? 3600*24*30 : 0; // 30 days Yii::app()->user->login($this->_identity,$duration); return true; } else return false; }
}
здесь вид
<?php /* @var $this SiteController */ /* @var $model LoginForm */ /* @var $form CActiveForm */ $this->pageTitle=Yii::app()->name . ' - Login'; $this->breadcrumbs=array( 'Login', ); ?> <h1>Login</h1> <p>Please fill out the following form with your login credentials:</p> <div class="form"> <?php $form=$this->beginWidget('CActiveForm', array( 'id'=>'login-form', 'enableClientValidation'=>true, 'clientOptions'=>array( 'validateOnSubmit'=>true, ), )); ?> <p class="note">Fields with <span class="required">*</span> are required.</p> <div> <?php echo CHtml::beginForm(); ?> <?php echo CHtml::errorSummary($form); ?> <div> <?php echo CHtml::activeLabel($form,'email'); ?> <?php echo CHtml::activeTextField($form,'email') ?> </div> <div> <?php echo CHtml::activeLabel($form,'password'); ?> <?php echo CHtml::activePasswordField($form,'password') ?> </div> <div> <?php echo CHtml::submitButton('Login'); ?> </div> <?php echo CHtml::endForm(); ?>
endWidget (); ?>
было бы хорошо, если бы вы включили большую часть ошибки – например, на какой строке она произошла и в каком файле, или в лучшем случае весь след. Из того, что я прочитал в вашем коде, кажется, что вы перезаписываете переменную $ form, которая на самом деле держите свою модель. В SiteController
вы инициализируете переменную $form
с LoginForm
модели LoginForm
. Затем в представлении вы делаете неправильный вызов:
<?php $form=$this->beginWidget('CActiveForm', array( 'id'=>'login-form', 'enableClientValidation'=>true, 'clientOptions'=>array( 'validateOnSubmit'=>true, ), )); ?>
Поэтому во всех вызовах CHtml вы помещаете виджет вместо переменной модели в функции
Поскольку вы не используете вывод виджета каким-либо конкретным способом, достаточно удалить $form = $this->widget...
и оставить его только как $this->widget..
Или заменить переменную для новенький.
<?php $myWidget = $this->beginWidget('CActiveForm', array( 'id'=>'login-form', 'enableClientValidation'=>true, 'clientOptions'=>array( 'validateOnSubmit'=>true, ), )); ?>