将过滤器注入 Zend_View
我希望通过构造函数注入在 MyFilter
中设置一些属性,但使用 Zend_View::addFilter(string $filter_class_name)
似乎不可能,因为它在使用时加载一个新实例。 MyFilter
实现 Zend_Filter_Interface
。
我能否以某种方式将过滤器的实例注入到Zend_View
的实例中?
关闭,因为它(希望)将被推送到 2.0,请参阅 JIRA 上的票 。
I wish to set some properties in MyFilter
with constructor injection but it seems impossible with Zend_View::addFilter(string $filter_class_name)
since it loads a new instance upon usage. MyFilter
implements Zend_Filter_Interface
.
Can I somehow inject an instance of a filter to an instance of Zend_View
?
Closing since it (hopefully) will be pushed into 2.0, see ticket on JIRA.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
您可以传递对象:
您可以从viewRenderer获取视图实例,例如使用staticHelper。
编辑:
另一种方法可能是:
You may pass object:
You may get view instance from viewRenderer, e.g. using staticHelper.
Edit:
The other method may be:
我不确定,但我认为这是不可能的。查看源代码
setFilter()
和addFilter()
仅接受 Filter Classname 作为字符串。您不能像在Zend_Form
中那样设置任何选项。不过你可以做的是:然后用 MyFilter::setOptions() 在需要的地方设置选项,这样当 Zend_View 实例化 Filter 实例时,它就得到了它所需要的正确运行过滤器。
I'm not certain, but I don't think it's possible. Looking at the sourcecode
setFilter()
andaddFilter()
only accept the Filter Classname as a string. You cannot set any options, like you can inZend_Form
for instance. What you could do though is:and then you set the options where needed with
MyFilter::setOptions()
, so whenZend_View
instantiates the Filter instance, it got what it needs to properly run the filter.您不能在 1.x 分支中,提交票证:
http://framework. zend.com/issues/browse/ZF-9718
You can't in the 1.x branch, ticket is filed:
http://framework.zend.com/issues/browse/ZF-9718
我们不能创建一个扩展
Zend_View
的自定义视图对象来重写addFilter()
方法来接受类或实例吗?然后重写_filter()
方法来处理我们存储的两种类型的过滤器 - 字符串和实例。Can't we create a custom view object extending
Zend_View
that overrides theaddFilter()
method to accept either a class or an instance. Then override the_filter()
method to deal with both types of filters - string and instance - that we have stored.为什么不将过滤器属性分配给视图,然后在设置视图时设置属性,或者直接在过滤函数中访问视图?例如,
然后在您的过滤器类中:
这很混乱,但它应该可以完成工作。
Why not assign the filter properties to the view, and then either set the properties when the view is set, or access the view directly in your filtering function? e.g.
and then in your filter class:
It's kludgy, but it should get the job done.