How to register a custom view helper with names?

I have a Zend Framework application with a user library called namespaced (PHP 5.3).

I want to register view helpers, but I cannot do this because of the namespace that I use in the view assistant.

Currently, in my bootstrap, I have the following to register the Assistant path:

protected function _initView() { $view = new Zend_View(); $view->addHelperPath( APPLICATION_PATH . "/../library/App/View/Helper", "App\View\Helper" ); } 

The error I am getting is:

Zend_Loader_PluginLoader_Exception: A plugin named "IsActive" was not found in the registry; paths used: App \ View \ Helper _:

Does anyone have an idea how to register name help helpers?

+4
source share
3 answers

In my configuration, I use:

 resources.view.helperPath.Glewz\View\Helper\ = APPLICATION_PATH "/../library/Glewz/View/Helper" 

One thing I found is that I need to include a constructor function, since the name of the view helper class and the public function are the same, it will use this function as a constructor. This will not be a problem if you use PHP 5.3.3 or higher - http://php.net/manual/en/language.oop5.decon.php - "Starting with PHP 5.3.3, methods with the same name as and the last element of a class name with names will no longer be construed as a constructor.This change does not affect classes that do not contain names.

+4
source

In my bootstrap, I use this:

 $view->addHelperPath(APPLICATION_PATH . '/../library/App/View/Helper', 'App_View_Helper'); 

Optional: You can overload the __call method in Zend_View_Abstract:

 if ( method_exists($helper, $name) ) { $methodName = $name; } else { $methodName = 'direct'; } return call_user_func_array(array($helper, $methodName), $args); 

Watch it

0
source

I think the default ZF autoloader cannot work with namespaces. You can try writing your own autoloader (or try using this one ) and register it by default.

0
source

Source: https://habr.com/ru/post/1345404/


All Articles