doubingling4706 2014-01-17 15:05
浏览 32
已采纳

适用于Zend的Site Wide Class,可用于操作,控制器和视图?

How can you create a site wide function for getting a role or region based on an 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/

I get the error:

Fatal error: Class 'Colouring' not found in

So then I thought Maybe a View_Helper is what I needed:

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/"

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

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.

I am using Zend 1.

  • 写回答

1条回答 默认 最新

  • dtotwai720621 2014-01-17 16:33
    关注

    first of all you have to rename the method: getColour() to Colouring() and make it public. Then try to call it from view like this:

    echo $this->Colouring('biz', 'bazz');
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 数学建模,尽量用matlab回答,论文格式
  • ¥15 昨天挂载了一下u盘,然后拔了
  • ¥30 win from 窗口最大最小化,控件放大缩小,闪烁问题
  • ¥20 易康econgnition精度验证
  • ¥15 msix packaging tool打包问题
  • ¥28 微信小程序开发页面布局没问题,真机调试的时候页面布局就乱了
  • ¥15 python的qt5界面
  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能