2012-02-20 44 views
1

我試圖讓我的自定義過濾器的工作......Zend框架的自定義過濾器在自己的libary目錄

我在AuthController下面的代碼:

<?php 
public function loginAction() 
{ 
    // Get db var 
    $db = $this->_getParam('db'); 

    // Load loginform 
    $loginForm = new Application_Form_Auth_Login(); 

    // Form posted? 
    if ($loginForm->isValid($_POST)) 
    { 
     // Setup adapter 
     $adapter = new Zend_Auth_Adapter_DbTable(
      $db, 
      'users', 
      'username', 
      'password' 
     ); 

     // Set identity and credential 
     $adapter->setIdentity($loginForm->getValue('username')); 
     $adapter->setCredential($loginForm->getValue('password')); 

     // Setup Zend_Auth and try to authenticate the user 
     $auth = Zend_Auth::getInstance(); 
     $result = $auth->authenticate($adapter); 

     // If authentication succeed 
     if ($result->isValid()) 
     { 
      $this->_helper->FlashMessenger('Inloggen geslaagd'); 
      $this->_redirect('/'); 
      return; 
     } 
     else 
     { 
      $this->_helper->FlashMessenger('Inloggen geslaagd'); 
     } 
    } 

    $this->view->loginForm = $loginForm; 
} 
?> 

形式的代碼:

<?php 
class Application_Form_Auth_Login extends Zend_Form 
{ 
    /** 
    * Default_Form_Auth_Login::init() 
    * 
    * Form which authenticates guests 
    * 
    * @return void 
    */ 
    public function init() 
    {  
    $this->setMethod('post');  

    $this->addElement('text', 'username', array(
     'label' => 'Gebruikersnaam:', 
     'required' => true, 
     'filters' => array('StringTrim'), 
    )); 

    $this->addElement('password', 'password', array(
     'label' => 'Wachtwoord:', 
     'required' => true, 
     'filters' => array('Pcw_Filter_Hash') 
    )); 

    $this->addElement('hash', 'formToken', array(
     'salt' => 'unique' 
    )); 

    $this->addElement('submit', 'submit', array(
     'ignore' => true, 
     'label' => 'Inloggen', 
    )); 

    } 
} 

我的自定義過濾器的代碼是:

<?php 

class Pcw_Filter_Hash implements Zend_Filter_interface 
{ 
    /** 
    * HashFilter::filter() 
    * 
    * @param string $value 
    * @return 
    */ 
    public function filter($value) 
    { 
    return hash('sha512', $value); 
    } 
} 

以這種方式使用時,我不斷收到此消息: 消息:在註冊表中找不到名爲'Pcw_Filter_Hash'的插件;使用的路徑:Zend_Filter_:Zend公司/過濾/

我發現文檔中關於設置的命名空間和添加路徑,但我不能得到任何工作......

任何人不會有我的問題一個可行的解決方案?這將非常讚賞!

在此先感謝!

回答

1

我寧願重寫你形成這樣一個元素:

$password = new Zend_Form_Element_Password("password"); 
$password->setLabel("Wachtwoord") 
     ->setRequired(true); 

$password->addFilter(new Pcw_Filter_Hash()); 

,但我不知道這可能工作:

$this->addElement('password', 'password', array(
     'label' => 'Wachtwoord:', 
     'required' => true, 
     'filters' => array(new Pcw_Filter_Hash()) 
    )); 

,你應該仔細檢查Pcwapplication.ini

定義

我希望你的問題很快解決:)

+0

謝謝你,但是在application.ini中定義Pcw對我來說不起作用。 這樣做的確如此:Zend_Loader_Autoloader :: getInstance(); $裝載機> registerNamespace( 'Pcw_'); 我沒有複製你寫我的表單元素的風格。 謝謝你的時間! – 2012-02-20 20:17:11

3

你必須的路徑從

<?php 
class Application_Form_Auth_Login extends Zend_Form 
{ 
    public function init() 
    {  
     // add the path where own filters are located 
     $this->addElementPrefixPath(
      'Pcw_Filter', 
      APPLICATION_PATH . '/../library/Pwc/Filter', 
      'filter' 
     ); 

     $this->setMethod('post'); 

     ... 
    } 
} 

添加到您的過濾器,你也許你改變路徑,以適應自己的應用程序的佈局。

+0

解決方案: $ loader = Zend_Loader_Autoloader :: getInstance(); $ loader-> registerNamespace('Pcw_'); – 2012-02-20 20:08:24

2

在你的application.ini加入這一行autoloaderNamespaces[] = "Pcw_"下一個確保該文件名爲Hash.php,它住在/application/libray/Pcw/Filter和類名需要保持Pcw_Filter_Hash如果你這樣做自動裝卸應該找到它。