将过滤器注入Zend_View

时间:2010-04-19 13:25:51

标签: php zend-framework dependency-injection zend-view zend-filter

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

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

关闭,因为它(希望)将被推入2.0,请参阅ticket on JIRA

5 个答案:

答案 0 :(得分: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;
     }
}

答案 1 :(得分:0)

我不确定,但我不认为这是可能的。查看源代码setFilter()addFilter()只接受Filter Classname作为字符串。您无法像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实例化Filter实例时,它会获得正确运行过滤器所需的内容。

答案 2 :(得分:0)

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

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

答案 3 :(得分:0)

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

答案 4 :(得分:0)

为什么不将过滤器属性分配给视图,然后在设置视图时设置属性,或者直接在过滤功能中访问视图? e.g。

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

然后在你的过滤器类中:

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

这很愚蠢,但它应该完成工作。