0

我希望MyFilter通过构造函数注入来设置一些属性,但这似乎是不可能的,Zend_View::addFilter(string $filter_class_name)因为它会在使用时加载一个新实例。MyFilter实现Zend_Filter_Interface

我可以以某种方式将过滤器的实例注入到 的实例中Zend_View

关闭,因为它(希望)将被推入 2.0,请参阅JIRA 上的票

4

5 回答 5

0

你可以传递对象:

$filter = new Your_Filter($params); // implements Zend_Filter_Interface
$view->addFilter($filter);

您可以从 viewRenderer 获取视图实例,例如使用 staticHelper。

编辑:

另一种方法可能是:

class MyFilterSetup extends MyFilter implements Zend_Filter_Interface 
{
     public function __construct($params)
     {
           $this->_params = $params;
           parent::__construct();
     }

     public function filter($string)
     {
         // .... $this->_params;
     }
}
于 2010-04-19T15:32:04.563 回答
0

我不确定,但我认为这不可能。查看源代码setFilter()addFilter()只接受过滤器类名作为字符串。您不能设置任何选项,例如可以设置Zend_Form。你可以做的是:

class MyFilter implements Zend_Filter_Interface 
{
     protected static $_config;
     public static setConfig(array $options)
     {
         self::_config = $options;
     }
     // ... do something with the options
}

然后你在需要的地方设置选项MyFilter::setOptions(),所以当Zend_View实例化过滤器实例时,它得到了正确运行过滤器所需的东西。

于 2010-04-20T07:30:04.987 回答
0

您不能在 1.x 分支中提交票证:

http://framework.zend.com/issues/browse/ZF-9718

于 2010-10-06T15:14:27.007 回答
0

我们不能创建一个自定义视图对象扩展Zend_View,覆盖addFilter()方法以接受类或实例。然后重写该_filter()方法来处理我们存储的两种类型的过滤器——字符串和实例。

于 2010-10-06T18:02:11.303 回答
0

为什么不将过滤器属性分配给视图,然后要么在设置视图时设置属性,要么直接在您的过滤函数中访问视图?例如

$view->assign('MyFilterProperty', 'fubar');

然后在您的过滤器类中:

public function setView($aView)
{
    $this->_property = $aView->MyFilterPropery;
}

这很笨拙,但它应该可以完成工作。

于 2010-10-29T21:50:48.587 回答