Zend Framework自定义筛选器位于自己的库目录中


Zend Framework Custom filter in own libary directory

我正在尝试让我的自定义过滤器工作。。。

我的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/Filter/

我找到了关于设置名称空间和添加路径的文档,但我什么都做不到。。。

有人能解决我的问题吗?这将是高度赞赏!

提前感谢!

您必须在from 中添加过滤器的路径

<?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');
        ...
    }
}

也许您必须更改路径以适应您自己的应用程序布局。

在application.ini中添加此行autoloaderNamespaces[] = "Pcw_",然后确保文件名为Hash.php,并且位于/application/libray/Pcw/Filter,并且如果您这样做,则类名需要保持为Pcw_Filter_Hash,这样自动加载器就会找到它。

我宁愿重写这样的表单元素:

$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())
      ));

您应该仔细检查Pcw是否在application.ini 中定义

我希望你的问题很快得到解决:)