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

Php Zend Framework自定义筛选器位于自己的库目录中,php,zend-framework,frameworks,filter,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 ($loginFor

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

我的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\u Filter\u Hash”的插件;使用的路径:Zend\u过滤器\ Zend/Filter/

我已经找到了关于设置名称空间和添加路径的文档,但我无法得到任何有用的东西

有人能有效解决我的问题吗?这将是高度评价


提前谢谢

我宁愿重写您的表单元素,如下所示:

$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())
      ));
您应该再次检查application.ini中是否定义了Pcw


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

我宁愿重写您的表单元素,如下所示:

$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())
      ));
您应该再次检查application.ini中是否定义了Pcw


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

您必须将路径添加到“发件人”中的筛选器中

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

        ...
    }
}

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

您必须将路径添加到“发件人”中的筛选器中

<?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\uu下一步,确保文件名为Hash.php,并且它位于/application/libray/Pcw/Filter,如果您这样做,则类名需要保留为Pcw\u Filter\u Hash,自动加载程序应该会找到它。

在application.ini中添加此行autoloaderNamespaces[]=Pcw_uu下一步,确保文件名为Hash.php,并且它位于/application/libray/Pcw/Filter,如果这样做,则类名需要保留为Pcw_Filter_Hash,自动加载程序应该会找到它。

解决方案:$loader=Zend_loader_autoloader::getInstance$loader->registerNamespace'Pcw_';解决方案:$loader=Zend\u loader\u Autoloader::getInstance$loader->registerNamespace'Pcw_';谢谢,但是在application.ini中定义Pcw对我不起作用。。这确实有效:Zend_Loader_Autoloader::getInstance$loader->registerNamespace'Pcw_';我确实复制了你写表单元素的风格。谢谢你抽出时间!谢谢,但是在application.ini中定义Pcw对我不起作用。。这确实有效:Zend_Loader_Autoloader::getInstance$loader->registerNamespace'Pcw_';我确实复制了你写表单元素的风格。谢谢你抽出时间!