Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/244.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 访问模型/控制器中的视图_Php_Zend Framework_View_Render - Fatal编程技术网

Php 访问模型/控制器中的视图

Php 访问模型/控制器中的视图,php,zend-framework,view,render,Php,Zend Framework,View,Render,我有一个类MyData.php,如下所示: class myData { function render() { $view = new Zend_View(); $view->str = 'This is string.'; echo $view->render('myview.phtml'); } } <div id='someid'><?= $this->str ?></div> 和myview.ph

我有一个类MyData.php,如下所示:

class myData {
  function render() {
    $view = new Zend_View();
    $view->str = 'This is string.';
    echo $view->render('myview.phtml');
  }
}
<div id='someid'><?= $this->str ?></div>
和myview.phtml文件:

class myData {
  function render() {
    $view = new Zend_View();
    $view->str = 'This is string.';
    echo $view->render('myview.phtml');
  }
}
<div id='someid'><?= $this->str ?></div>

MyData.phpmyview.phtml位于同一目录中。

您正在创建一个新的Zend_视图实例。你不应该这样做。要获取现有视图实例,可以执行以下操作:

$view = Zend_Controller_Front::getInstance()->getParam('bootstrap')->getResource('view');

另外,我认为视图脚本路径应该相对于
应用程序路径/views/scripts
文件夹。

您正在创建一个新的Zend\u视图实例。你不应该这样做。要获取现有视图实例,可以执行以下操作:

$view = Zend_Controller_Front::getInstance()->getParam('bootstrap')->getResource('view');

另外,我认为视图脚本路径应该是相对于
应用程序路径/views/scripts
文件夹的。

如果您使用的是完整的MVC堆栈,您最好只为这种类型的东西创建一个视图帮助器。。。或者简单地使用局部视图辅助对象传递对象并将对象传递给它

例如,使用现有的Zend_View_Helper_Partial

在控制器中,创建myData对象并将其指定给视图:

public function indexAction()
{
   $this->view->mydata = new MyData();
}
在操作的视图中:

echo $this->partial('myview.phtml', array('obj' => $this->mydata));
然后在
myview.phtml
中,您可以执行以下操作:

<div><?php echo $this->obj->somevar ?></div>

对于您的示例,看起来您根本不需要myData对象,您只需将
str
变量分配给视图,并将其传递给分部,而无需创建对象


如果您使用的是完整的MVC堆栈,那么您应该阅读…

,您最好只为这种类型的东西创建一个视图帮助器。。。或者简单地使用局部视图辅助对象传递对象并将对象传递给它

例如,使用现有的Zend_View_Helper_Partial

在控制器中,创建myData对象并将其指定给视图:

public function indexAction()
{
   $this->view->mydata = new MyData();
}
在操作的视图中:

echo $this->partial('myview.phtml', array('obj' => $this->mydata));
然后在
myview.phtml
中,您可以执行以下操作:

<div><?php echo $this->obj->somevar ?></div>

对于您的示例,看起来您根本不需要myData对象,您只需将
str
变量分配给视图,并将其传递给分部,而无需创建对象

你应该读…

我是这样做的:

<?php
    $obj = new myData ();
    $obj->render(); // it should be <div id='someid'>This is string.</div>
?>
我将我的myview.phtml更改为myview.php

在myData类渲染函数中:

class myData {
  function render() {
    $view = new Zend_View();
    $view->setScriptPath( "/Directory/Path/For/myview/php/file" );
    $view->str = 'This is string.';
    echo $view->render('myview.php');
  }
}
所有的事情都按照我的要求进行。我缺少
$view->setScriptPath($path)在我的代码中

帮助:

class myData {
  function render() {
    $view = new Zend_View();
    $view->str = 'This is string.';
    echo $view->render('myview.phtml');
  }
}
<div id='someid'><?= $this->str ?></div>
    • 我是这样做的:

      <?php
          $obj = new myData ();
          $obj->render(); // it should be <div id='someid'>This is string.</div>
      ?>
      
      我将我的myview.phtml更改为myview.php

      在myData类渲染函数中:

      class myData {
        function render() {
          $view = new Zend_View();
          $view->setScriptPath( "/Directory/Path/For/myview/php/file" );
          $view->str = 'This is string.';
          echo $view->render('myview.php');
        }
      }
      
      所有的事情都按照我的要求进行。我缺少
      $view->setScriptPath($path)在我的代码中

      帮助:

      class myData {
        function render() {
          $view = new Zend_View();
          $view->str = 'This is string.';
          echo $view->render('myview.phtml');
        }
      }
      
      <div id='someid'><?= $this->str ?></div>
      

      @prodigitalson:你能提供一个符合我上述要求的例子吗。@prodigitalson:你能提供一个符合我上述要求的例子吗?这在你使用Zend Framework以精简引导运行cronjobs的情况下也适用。这在你使用Zend Framework以精简引导运行cronjobs的情况下也适用剥离引导。