Zend Framework自定义过滤器位于自己的libary目录中

时间:2012-02-20 19:36:31

标签: php zend-framework frameworks filter

我正在努力让我的自定义过滤器工作......

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

我找到了有关设置命名空间和添加路径的文档,但我无法正常工作......

有没有人能解决我的问题?这将非常值得赞赏!

提前致谢!

3 个答案:

答案 0 :(得分:3)

您必须在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');

        ...
    }
}

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

答案 1 :(得分:2)

在你的application.ini中添加以下行autoloaderNamespaces[] = "Pcw_",确保文件名为Hash.php,并且它位于/application/libray/Pcw/Filter,类名需要保留Pcw_Filter_Hash如果你这样做,自动加载器应该找到它。

答案 2 :(得分: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())
      ));

您应该仔细检查Pcw

中是否定义了application.ini

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