简体   繁体   English

可以在操作,控制器和视图中使用的Zend站点级类吗?

[英]Site Wide Class for Zend that can be used in actions, controllers and views?

How can you create a site wide function for getting a role or region based on an id. 如何创建站点范围的功能,以根据ID获得角色或区域。 Also things like returning a specific colour based on a calculation of (actual/target), in a view. 另外,在视图中还可以根据(实际/目标)的计算返回特定的颜色。 I want to use it in a view and action. 我想在视图和动作中使用它。

I have tried, creating a class and setting it up in library: 我已经尝试过,创建一个类并在库中进行设置:

class Colouring 
{
    /*
     * Array of the class for specifically the GEP portal theme
     */
    private $colours_class = array( 0 => 'color-grey',
                                1 => 'color-grey',
                                2 => 'color-orange',
                                3 => 'color-blue',
                                4 => 'color-green'
                            );

    /*
     * Returns colour based on actual and target
     */
    private static function getColour($actual, $target) 
    {
        ...return someValue

        }
}

Path: Zend_Site/library/ 路径: Zend_Site/library/

I get the error: 我得到错误:

Fatal error: Class 'Colouring' not found in

So then I thought Maybe a View_Helper is what I needed: 因此,我以为也许需要View_Helper:

So I changed path to: My/View/Helper/Colouring.php and Class to: My_View_Helper_Colouring added this to config.ini: resources.view.helperPath.My_View_Helper_ = "My/View/Helper/" 因此,我将路径更改为: My/View/Helper/Colouring.php ,将类更改为: My_View_Helper_Colouring将其添加到config.ini: resources.view.helperPath.My_View_Helper_ = "My/View/Helper/"

I use: 我用:

$viewHelperObj = $this->view->getHelper('Colouring');
$viewHelperObj->getColour($this->value, $this->divisor);

Gives: Fatal error: Call to a member function getHelper() on a non-object 给定: Fatal error: Call to a member function getHelper() on a non-object

I tried also: 我也尝试过:

$this->getColour($this->value, $this->divisor);

Which Gives: 这使:

Plugin by name 'GetColour' was not found in the registry

So not really getting the idea of the correct practice to use to add static methods for things that are site wide, or even jsut a site wide class and the way to register. 因此,并没有真正获得用于在站点范围内甚至为站点范围内的类和注册方式添加静态方法的正确方法的想法。 Would I use Plugins, View helpers, Action helpers or just striaght up classes. 我会使用插件,View帮助器,Action帮助器还是只是上课。

I am using Zend 1. 我正在使用Zend 1。

first of all you have to rename the method: getColour() to Colouring() and make it public. 首先,您必须将方法重命名: getColour() to Colouring() getColour() to Colouring()并将其公开。 Then try to call it from view like this: 然后尝试从这样的视图调用它:

echo $this->Colouring('biz', 'bazz');

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM