您的位置:首页 > 编程语言 > PHP开发

Zend Framework教程-Zend_Helpers-视图助手-创建自己的视图助手-demos2

2012-05-20 12:17 489 查看
/helper_demo1/application/controllers/IndexController.php

<?php

class IndexController extends Zend_Controller_Action
{

    public function init()
    {
        /* Initialize action controller here */
    }
    public function testAction(){
    	
    	//$this->view->addHelperPath('/home/coder/www/helper_demo1/library/Test/Helper', 'Test_Helper');
    	//或者因为在includepath已经设置library,所以可以直接:
    	$this->view->addHelperPath('Test/Helper', 'Test_Helper');
    }


/helper_demo1/library/Test/Helper/MyHelper.php即绝对路径是/home/coder/www/helper_demo1/library/Test/Helper/MyHelper.php

文件内容:

<?php
require_once 'Zend/View/Interface.php';

/**
 * MyHelper helper
 *
 * @uses viewHelper Test_Helper
 */
class Test_Helper_MyHelper {
	
	/**
	 *
	 * @var Zend_View_Interface
	 */
	public $view;
	
	/**
	 */
	public function myHelper() {
		 
		return "this my helper return !".rand(1, 10);
	}
	
	/**
	 * Sets the view field
	 * 
	 * @param $view Zend_View_Interface        	
	 */
	public function setView(Zend_View_Interface $view) {
		$this->view = $view;
	}
}


对应action的phtml调用方法

<?php 
echo $this->myHelper();
?>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: