2012-02-13 83 views
0

我在哪里将用户定义的函数放在zend框架中。这些函数将用于整个框架中的许多控件,视图或模型。我是否需要将此转换为实用程序类?或者我可以保留它作为一组函数,并将其包含在index.php中。zend中的用户定义函数

这是什么最佳实践?

回答

1

通常你会把你的函数放到自动加载器库的类中。使用ZF的命名约定使生活更轻松。
调整您的application.ini以添加命名空间。

例子:

做动作助手的控制器和视图助手的意见:

在你的库文件夹被设置

//application.ini 
autoloaderNamespaces[] = "My_" 



//this would equate to the folder My in the folder library 
    /application 
     /library 
     /My 

//any class you built would be named My_Classname and be called in your app by Classname() 
<?php 
    class My_Classname { 
    public function myFunction() { 
    } 
    } 

    //in your conrtoller for example you might call 

    public function indexAction() { 
    $class = new My_Classname(); 
    $class->myFunction(); 

    //or if you declared myFunction() static... 
    $class = My_Classname::myFunction(); 
    } 
+0

非常感谢您提出这个想法。我刚刚实施它,它工作正常。 – codlib 2012-02-13 06:18:31

0

按照ZF目录结构使它在set_include_path中:

create library/My/View/Helper/Common.php 

象下面这样:

class My_View_Helper_Common extends Zend_View_Helper_Abstract 
    { 
     public function common() 
     { 
      return $this; 
     } 
     public function getCity($id) 
     { 
      $registry = Zend_Registry::getInstance(); 
      $DB = $registry['DB']; 

      $result = $DB->fetchPairs("select * from firm_dtl"); 

      return $result; 
     } 
} 

或查看呼叫:

$this->common()->getCity($id); 

同样的过程来回动作助手:

让库/我的/动作/助手/的common.php