将过滤器注入 Zend_View

发布于 2024-08-29 18:18:23 字数 391 浏览 12 评论 0原文

我希望通过构造函数注入在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

肥爪爪 2024-09-05 18:18:23

您可以传递对象:

$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;
     }
}

You may pass object:

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

You may get view instance from viewRenderer, e.g. using staticHelper.

Edit:

The other method may be:

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

     public function filter($string)
     {
         // .... $this->_params;
     }
}
够钟 2024-09-05 18:18:23

我不确定,但我认为这是不可能的。查看源代码 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 实例时,它就得到了它所需要的正确运行过滤器。

I'm not certain, but I don't think it's possible. Looking at the sourcecode setFilter() and addFilter() only accept the Filter Classname as a string. You cannot set any options, like you can in Zend_Form for instance. What you could do though is:

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

and then you set the options where needed with MyFilter::setOptions(), so when Zend_View instantiates the Filter instance, it got what it needs to properly run the filter.

恍梦境° 2024-09-05 18:18:23

您不能在 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

太傻旳人生 2024-09-05 18:18:23

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

Can't we create a custom view object extending Zend_View that overrides the addFilter() 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.

拥抱影子 2024-09-05 18:18:23

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

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

然后在您的过滤器类中:

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

这很混乱,但它应该可以完成工作。

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.

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

and then in your filter class:

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

It's kludgy, but it should get the job done.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文