从 Yii 中的布局/视图文件调用函数 [英] Calling a function from the layout/view file in Yii

查看:16
本文介绍了从 Yii 中的布局/视图文件调用函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是 Yii 框架的新手,我需要定义一个从/protected/views/layouts/main.php 页面调用的函数.我可以定义它吗(控制器/模型),以便我可以在函数中定义相应的动作.

I am new to Yii framework and i need to define a function which is being called from /protected/views/layouts/main.php page. can i knw were to define it(Controller/model), so that i can get the corresponding action defined within the function.

推荐答案

根据函数的作用以及您需要它的可见程度,您可以执行至少两件事之一:

Depending on what the function does and how visible you need it to be, you can do one of at least two things:

  1. 扩展 CController 基类,在那里定义函数,然后从这个新类派生每个控制器类.要调用该函数,您需要运行 $this->myFunction();)
  2. 将函数放入模型中,在控制器动作函数中实例化该模型的对象,然后在视图中通过$this->myModelObject->myFunction();.

这篇关于从 Yii 中的布局/视图文件调用函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆