【问题标题】:Zend Framework Custom filter in own libary directoryZend Framework 自定义过滤器在自己的库目录中
【发布时间】:2012-03-11 03:48:24
【问题描述】:

我正在尝试使我的自定义过滤器工作...

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

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

有人对我的问题有有效的解决方案吗?这将是高度赞赏!

提前致谢!

【问题讨论】:

    标签: php zend-framework frameworks filter


    【解决方案1】:

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

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

    【讨论】:

    • 解决方案:$loader = Zend_Loader_Autoloader::getInstance(); $loader->registerNamespace('Pcw_');
    【解决方案2】:

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

    【讨论】:

      【解决方案3】:

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

      $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 中定义

      希望你的问题早日解决:)

      【讨论】:

      • 谢谢,但在 application.ini 中定义 Pcw 对我不起作用。这确实起作用:Zend_Loader_Autoloader::getInstance(); $loader->registerNamespace('Pcw_');我确实复制了您编写表单元素的风格。感谢您的宝贵时间!
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-06-21
      • 2013-06-02
      相关资源
      最近更新 更多