我一直在做一個教程,但是我的代碼根本無法工作。我有一個錯誤 「找不到網頁」使用Zend Framework 1.2進行身份驗證
我的形式:
<?php
class Application_Form_LoginForm extends Zend_Form {
public function init() {
$this->setName('add_user');
$email = new Zend_Form_Element_Text('email', 'Adresse Email : ');
$password = new Zend_Form_Element_Password('password');
$password->setLabel('Mot de pase : ')
->setRequired(true)
->addFilter('StripTags')
->addFilter('StringTrim');
$submit = new Zend_Form_Element_Submit('submit');
$submit->setAttrib('id', 'submitbutton')
->setLabel('Se connecter');
$elements = array($email, $password, $submit);
$this->addElements($elements);
$this->setDecorators(array(
'FormElements',
array('HtmlTag', array('tag' => 'dl', 'class' => 'zend_form')),
array('Errors', array('placement' => 'apend')),
'Form'
));
}
}
?>
我的控制器:
<?php
class LoginController extends Zend_Controller_Action {
public function init() {
/* Initialize action controller here */
Zend_Auth::getInstance()->clearIdentity();
}
public function LoginAction() {
$form = new Application_Form_LoginForm();
$this->view->form = $form;
if ($this->_request->isPost()) {
$formData = $this->_request->getPost();
if ($form->isValid($formData)) {
$email = $form->getValue('email');
$password = $form->getValue('password');
$authAdapter = new Zend_Auth_Adapter_DbTable(Zend_Db_Table::getDefaultAdapter());
$authAdapter->setTableName('users')
->setIdentityColumn('email')
->setCredentialColumn('password')
->setCredentialTreatment('MD5(?)')
->setIdentity($email)
->setCredential($password);
$authAuthenticate = $authAdapter->authenticate();
if ($authAuthenticate->isValid()) {
$storage = Zend_Auth::getInstance()->getStorage();
$storage->write($authAdapter->getResultRowObject(null, 'password'));
$this->_helper->redirector('index', 'index');
} else {
$form->addError('Il nexiste pas dutilisateur avec ce mot de passe');
}
}
}
$this->render('index');
}
public function indexAction() {
$this->_forward('Login');
}
public function logoutAction() {
Zend_Auth::getInstance()->clearIdentity();
$this->_helper->redirector('index', 'index');
}
public function preDispatch() {
if (Zend_Auth::getInstance()->hasIdentity()) {
if ('logout' != $this->getRequest()->getActionName()) {
$this->_helper->redirector('index', 'index');
}
} else {
if ('logout' == $this->getRequest()->getActionName()) {
$this->_helper->redirector('index');
}
}
}
}
?>
我的助手:
<?php
class Zend_View_Helper_ProfileLink extends Zend_View_Helper_Abstract {
public function profileLink() {
$helperUrl = new Zend_View_Helper_Url ();
$auth = Zend_Auth::getInstance();
if ($auth->hasIdentity()) {
$username = $auth->getIdentity()->prenom . ' ' . strtoupper(substr($auth->getIdentity()->nom, 0, 1)) . '.';
$logoutLink = $helperUrl->url(array('action' => 'logout', 'controller' => 'login'));
return 'Salut ' . $username . ' (<a href="' . $logoutLink . '">Logout</a>)';
}
$loginLink = $helperUrl->url(array('action' => 'login', 'controller' => 'Login'));
return '<a href="' . $loginLink . '">Login</a>';
}
}
?>
在我的應用/佈局/腳本/layout.phtml
echo $this->profileLink();
「登錄」確實出現在我的視圖中。這是當我點擊它,我有錯誤「找不到網頁」提前
感謝您的幫助產生
HTML代碼「登錄」
</html><a href="/ZendProject/tutoriel-zf/public/login/login">Login</a> </div>
有一件事我沒有注意到,在這段代碼中沒有任何地方設置表單_action_或_method_。從基礎開始,看看會發生什麼。 – RockyFord