我刚接触Zend框架,有一个基础问题。
假设我在使用layout.phtml或任何脚本的index.phtml。
当我使用"$this->"时,我指的是哪个实例?
我在一本书中读到以下内容: "$this在模板文件中可用,它是访问Zend_View功能的入口"。
这是否意味着我可以访问library/Zend/View/Helpers目录中任何文件内的任何类的方法?
如果这个问题太愚蠢或者太简单,请原谅。
谢谢
我刚接触Zend框架,有一个基础问题。
假设我在使用layout.phtml或任何脚本的index.phtml。
当我使用"$this->"时,我指的是哪个实例?
我在一本书中读到以下内容: "$this在模板文件中可用,它是访问Zend_View功能的入口"。
这是否意味着我可以访问library/Zend/View/Helpers目录中任何文件内的任何类的方法?
如果这个问题太愚蠢或者太简单,请原谅。
谢谢
$this->
在视图模板中是对您在控制器中创建的 Zend_view 对象的引用。
尝试在模板中使用 var_dump($this)
或 print_r($this)
(在 print_r 前加上 <pre>
以获得更好的格式化)。变量转储可能会帮助您更好地理解发生了什么。
顺便提一下,你同样可以直接访问应用程序/视图/过滤器目录中放置的任何 过滤器,正如你可以从Zend/Controller/Action.php中的initView()的docblock看到的那样。
/**
* Initialize View object
*
* Initializes $view if not otherwise a Zend_View_Interface.
*
* If $view is not otherwise set, instantiates a new Zend_View
* object, using the 'views' subdirectory at the same level as the
* controller directory for the current module as the base directory.
* It uses this to set the following:
* - script path = views/scripts/
* - helper path = views/helpers/
* - filter path = views/filters/
*
* @return Zend_View_Interface
* @throws Zend_Controller_Exception if base view directory does not exist
*/
public function initView()
通常情况下,您会在控制器操作中使用类似以下方式将某些数据分配给视图对象:
$form = My_Form;
//assign My_Form to the view object
$this->view->form = $form;
//this bit of code would display your whole form in the view script
//along with any layout information contained in your layout file
<?php echo $this->form ?>
同时,可以从Bootstrap中将项目分配给视图对象,并且这些项目将对布局或视图脚本可用。以下是一个例子:
protected function _initView() {
//Initialize view
$view = new Zend_View();
//get doctype from application.ini
$view->doctype(Zend_Registry::get('config')->resources->view->doctype);
$view->headTitle('Our Home');
//get content-type from application.ini
$view->headMeta()->appendHttpEquiv('Content-Type',
Zend_Registry::get('config')->resources->view->contentType);
//add css files
$view->headLink()->setStylesheet('/css/blueprint/screen.css');
$view->headLink()->appendStylesheet('/css/blueprint/print.css', 'print');
$view->headLink()->appendStylesheet('/css/master.css');
$view->headLink()->appendStylesheet('/css/main.css');
$view->headLink()->appendStylesheet('/css/nav.css');
//add it to the view renderer
$viewRenderer = Zend_Controller_Action_HelperBroker::getStaticHelper(
'ViewRenderer');
$viewRenderer->setView($view);
//Return it, so that it can be stored by the bootstrap
return $view;
现在,可以通过以下方式在layout.phtml中访问此数据:
<?php echo $this->doctype() . "\n"; ?>
<html>
<head>
<?php echo $this->headMeta() . "\n" ?>
<?php echo $this->headLink() . "\n" ?>
<!--[if lt IE 8]>
<link rel="stylesheet" href="/css/blueprint/ie.css" type="text/css" media="screen, projection" />
<![endif] -->
</head>
为了完整起见,这里是 $this 的 PHP 手册版本:
在类方法中,可以使用 $this->property(其中 property 是属性的名称)的形式访问属性、常量和方法,除非在静态类方法的上下文中访问静态属性,在这种情况下,使用 self::$property 的形式访问。有关更多信息,请参见静态关键字。
伪变量 $this 在任何类方法内部可用,当该方法从对象上下文中调用时。$this 是对调用对象的引用(通常是方法所属的对象,但如果从辅助对象的上下文中静态调用该方法,则可能是另一个对象)。
这并不是一个完整的解释,但我希望它能让您有所启发。
如所述,视图中的$this
是Zend_View的实例。
请参见Zend_View类中的render方法。
public function render($name)
{
// find the script file name using the parent private method
$this->_file = $this->_script($name);
unset($name); // remove $name from local scope
ob_start();
$this->_run($this->_file);
return $this->_filter(ob_get_clean()); // filter output
}
基本上,ZF操作辅助程序(ViewRnderer)创建一个Zend_View实例,并通过传递视图文件的名称(index.phtml)调用渲染方法。
$view = new Zend_View(); $view->render('index.phtml');
正如您所看到的,输出缓冲区在渲染方法中使用ob_start。 它在Zend_View类的上下文中加载index.phtml文件,因此可以在其中使用$this作为其代码是该类的一部分。
$this
将帮助您使用在项目的 application/view/helper
目录中定义的辅助函数。您可以通过在初始化器中将它们与 zend_helper
绑定来在项目的 .phtml
或视图文件中的任何位置使用所有这些辅助函数。