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

/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();
?>




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值