需要帮助弄清楚如何编写我的 zend 视图助手

发布于 2024-09-12 07:03:42 字数 1479 浏览 9 评论 0原文

一般来说,我对 Zend Framework 和 MVC 相当陌生,所以我正在寻求一些建议。我们有一个基本控制器类,其中有一些方法来获取一些用户信息、帐户配置等。

所以我使用其中一些方法在各种控制器操作中编写代码,但现在我想避免重复此代码更进一步,我想将此代码放在控制器之外并放在视图助手中,因为它主要是输出一些 JavaScript。因此控制器中的代码如下所示:

$obj= new SomeModel ( $this->_getModelConfig () );
$states = $obj->fetchByUser ( $this->user->getId() );
//Fair amount of logic here using this result to prepare some javascript that should be sent to the view...

$this->_getModelConfig 和 $this->user->getId() 是我可以在控制器中执行的操作,现在我的问题是最好的方法是什么一旦我将此代码移出控制器,将该信息传递给视图助手?

我是否应该在控制器中调用这些方法并将结果存储到视图中并让助手从那里获取它?

我想到的另一个选择是向助手添加一些参数,如果参数被传递,那么我将它们存储在助手的属性中并返回,并且当在不传递参数的情况下调用时,它会执行工作。所以它看起来像这样:

从控制器:

$this->view->myHelper($this->user->getId(), $this->_getModelConfig());

从视图:

<?= $this->myHelper(); %>

助手:

class Zend_View_Helper_MyHelper extends Zend_View_Helper_Abstract
{
    public $userId = '';
    public $config = null;
    public function myHelper ($userId = null, $config = null)
    {
        if ($userId) {
            $this->userId = $userId;
            $this->config = $config;
        } else {
            //do the work
            $obj = new SomeModel($this->config);
            $states = $obj->fetchByUser($this->userId);
            //do the work here  
        }
        return $this;
    }
}

欢迎任何建议!

I'm fairly new to Zend Framework and MVC in general so I'm looking for some advice. We have a base controller class in which we have some methods to obtain some user information, account configurations, etc.

So I'm using some of those methods to write out code in various controllers actions, but now I want to avoid duplicating this code and further more I would like to take this code outside of the controller and in a view helper as it is mainly to output some JavaScript. So the code in the controller would look like this:

$obj= new SomeModel ( $this->_getModelConfig () );
$states = $obj->fetchByUser ( $this->user->getId() );
//Fair amount of logic here using this result to prepare some javascript that should be sent to the view...

The $this->_getModelConfig and $this->user->getId() are things that I could do in the controller, now my question is what is the best way to pass that information to the view helper once i move this code out of the controller ?

Should I just call these methods in the controller and store the results into the view and have the helper pick it up from there ?

Another option I was thinking of was to add some parameters to the helper and if the parameters are passed then I store them in properties of the helper and return, and when called without passing the parameters it performs the work. So it would look like this:

From controller:

$this->view->myHelper($this->user->getId(), $this->_getModelConfig());

From view:

<?= $this->myHelper(); %>

Helper:

class Zend_View_Helper_MyHelper extends Zend_View_Helper_Abstract
{
    public $userId = '';
    public $config = null;
    public function myHelper ($userId = null, $config = null)
    {
        if ($userId) {
            $this->userId = $userId;
            $this->config = $config;
        } else {
            //do the work
            $obj = new SomeModel($this->config);
            $states = $obj->fetchByUser($this->userId);
            //do the work here  
        }
        return $this;
    }
}

Any advice is welcomed!

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

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

发布评论

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

评论(2

九八野马 2024-09-19 07:03:42

首先,ASP 风格的结束标记位于“$this->myHelper(); %>”这是不好的做法,话虽如此,最好将逻辑保留在模型中,而控制器仅用于调用模型,获取结果并将其吐出到视图中以供查看。

我要做的是,如果我只是想将一堆值传递给视图,我会将它们填充到关联数组中并将它们发送出去。

无论如何,你不应该做你的...

“//这里使用这个结果的大量逻辑来准备一些应该发送到视图的javascript...”

控制器中的部分,我建议你制作一个新模型它会为您完成逻辑工作,您只需在控制器中调用您的模型,传递所需的参数,然后将结果吐给视图。

Firstly the ASP style ending tag here at "$this->myHelper(); %>" is bad practice, with that said it is more advisable to keep the logic in the model and the controller just being used to call the model, get the results and spit that to the view for viewing.

what I would do is, if i simply want to pass a bunch of values to the view, i stuff them in an associative array and send them over.

anyway you should not be doing your ...

"//Fair amount of logic here using this result to prepare some javascript that should be sent to the view..."

part in the controller, I would advice you to make a new model that does that logic stuff for you, and you just call your model in the controller pass it what ever arguments that are needed and then spit the result of that to the view.

饮惑 2024-09-19 07:03:42

最好的方法是通过控制器从模型获取数据,然后传递到视图。但如果您确实需要一个自定义助手来回显视图部分,我们只有在您准确说出您想要执行的操作时才会知道。

如果您的助手中已经有此逻辑,请尝试仅在视图中传递参数 myhelper($this->params); ?>

您可能也想看看这种方法:

// In your view to put javascript in the header
// You can loop trought your data and then use it to generate the javascript.
<?php $this->headScript()->captureStart(); ?>
    $().ready(function(){
        $('#slideshow').cycle({ 
            fx:     'fade', 
            speed:  1000, 
            timeout: 6500, 
            pager:  '#nav'
        });
    });
<?php $this->headScript()->captureEnd() ?>  

The best way is to get the data from your model throught your controller, and then pass to the view. But if you really need a custom helper to echo the view parts, we only will know if you say exactly what you're trying to do.

If you already have this logic in a helper, try to just pass the parameters in your view myhelper($this->params); ?>

You may want take a look at this approach too:

// In your view to put javascript in the header
// You can loop trought your data and then use it to generate the javascript.
<?php $this->headScript()->captureStart(); ?>
    $().ready(function(){
        $('#slideshow').cycle({ 
            fx:     'fade', 
            speed:  1000, 
            timeout: 6500, 
            pager:  '#nav'
        });
    });
<?php $this->headScript()->captureEnd() ?>  
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文