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

发布于 2024-10-17 10:44:47 字数 756 浏览 4 评论 0原文

我有一个像这样的 MyData.php 类:

class myData {
  function render() {
    $view = new Zend_View();
    $view->str = 'This is string.';
    echo $view->render('myview.phtml');
  }
}

和一个 myview.phtml 文件:

<div id='someid'><?= $this->str ?></div>

在另一个视图中,我正在做这样的事情:

<?php
    $obj = new myData ();
    $obj->render(); // it should be <div id='someid'>This is string.</div>
?>

它给了我以下异常:

Message: no view script directory set; unable to determine location for view script

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

I have a class MyData.php like this:

class myData {
  function render() {
    $view = new Zend_View();
    $view->str = 'This is string.';
    echo $view->render('myview.phtml');
  }
}

and a myview.phtml file:

<div id='someid'><?= $this->str ?></div>

In another view I am doing something like this:

<?php
    $obj = new myData ();
    $obj->render(); // it should be <div id='someid'>This is string.</div>
?>

It is giving me following exception:

Message: no view script directory set; unable to determine location for view script

MyData.php and myview.phtml are in same directory.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

往日 2024-10-24 10:44:47

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

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

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

You are creating a new Zend_View instance. You should not do this. To get the existing view instance you could do as follows:

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

Also, I think that the view script path should be relative to APPLICATION_PATH/views/scripts folder.

年华零落成诗 2024-10-24 10:44:47

我是这样做的:

我将 myview.phtml 更改为 myview.php

str ?>

在 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);

帮助:

I did it like this:

I changed my myview.phtml to myview.php

<div id='someid'><?= $this->str ?></div>

In myData class render function:

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');
  }
}

And all things are working as I asked in question. I was missing $view->setScriptPath($path); in my code.

Help:

智商已欠费 2024-10-24 10:44:47

如果您使用完整的 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 变量分配给视图并将其传递给局部视图,而不是创建对象。

您应该阅读 Zend_View 文档。 。

If you are usin the full MVC stack its youre better off just creating a view helper for this type of thing... or simply passing the using the Partial view helper and passing your object to it.

For example with the exisiting Zend_View_Helper_Partial....

in your controller create the myData object and assign it to the view:

public function indexAction()
{
   $this->view->mydata = new MyData();
}

in the view for the action:

echo $this->partial('myview.phtml', array('obj' => $this->mydata));

Then in your myview.phtml you can do:

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

For your example it looks like you dont even need the myData object at all you can just assign the str variable to the view and pass it along to the partial instead of creating an object.

You should read the Zend_View docs...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文