views:

145

answers:

5

Hi!

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.

A: 

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;
     }
}
takeshin
Your code is invalid. `Zend_View::addFilter()` takes a string (or array of strings) as argument and loads the classes dynamically later. If you in fact can get your code to run, feel free to write some more lines of code to cover it :) Thanks.
chelmertz
The new code example works like the first, still no chance to actually get a value into it before it's used inside Zend_View.
chelmertz
A: 

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.

Gordon
That is nearly one option but not very elegant. What if I would want to add the same filter twice, but with different settings?
chelmertz
@chelmertz Then you likely would have to customize `Zend_View` to do what you want to do. Or store the number of instantiations the filter went through as another static property and add one config per instance statically. That ain't pretty, but would work.
Gordon
@Gordon I'd rather use the output of `Zend_View::render()` and apply a filter on that, but it takes away the power of having associated fitlers with the view. Gonna push this further if there isn't something we've missed.
chelmertz
A: 

You can't in the 1.x branch, ticket is filed:

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

chelmertz
A: 

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.

David Weinraub
@DavidW: I think there's a design error and that those should be revised within the framework in itself. But I think it's a valid solution so.. why not patch it? :)
chelmertz
Gotcha, thanks!
David Weinraub
A: 

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.

Duncan