处理每个综合浏览量所需的应用程序范围数据的正确方法 [英] Correct way to deal with application-wide data needed on every pageview

查看:70
本文介绍了处理每个综合浏览量所需的应用程序范围数据的正确方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在从事一个大型的Web应用程序的开发,该应用程序是用PHP编写的,并且基于MVC框架,在架构方面与Zend框架具有广泛的相似性.

I am currently involved in the development of a larger webapplication written in PHP and based upon a MVC-framework sharing a wide range of similarities with the Zend Framework in terms of architecture.

用户登录后,我有一个地方应该显示当前用户虚拟点的余额.该显示需要位于每个控制器的每个页面上.

When the user has logged in I have a place that is supposed to display the balance of the current users virtual points. This display needs to be on every page across every single controller.

您将代码放置在何处以获取侧边模型数据,这些数据不是控制器特定的,而是需要在每个综合浏览量的站点范围内进行布局,而与当前控制器无关? MVC或ZF如何处理-头这样做吗?其余的人呢?

Where do you put code for fetching sidewide modeldata, that isn't controller specific but needs to go in the sitewide layout on every pageview, independently of the current controller? How would the MVC or ZF-heads do this? And how about the rest of you?

我曾考虑过在用户登录并将其存储在会话中时加载余额,但是由于余额经常更改,这似乎不正确-每次加载页面时都需要对其进行检查和更新.我还考虑过通过将提取例程添加到每个控制器来做到这一点,但这似乎也不对,因为这会导致代码重复.

I thought about loading the balance when the user logs in and storing it in the session, but as the balance is frequently altered this doesn't seem right - it needs to be checked and updated pretty much on every page load. I also thought about doing it by adding the fetching routine to every controller, but that didn't seem right either as it would result in code-duplication.

推荐答案

好吧,没错,为每个控制器提供例程将是代码重复,并且不会使代码可重用.

Well, you're right, having routines to every controller would be a code-duplication and wouldn't make your code reusable.

与您的问题注释中所建议的不同,我不会选择基本控制器,因为基本控制器不是一种好习惯(在大多数情况下),并且Zend Framework实现了 Action Helpers 避免它们.

Unlike suggested in your question comments, I wouldn't go for a a base controller, since base controllers aren't a good practice (in most cases) and Zend Framework implements Action Helpers in order to to avoid them.

如果局部视图是整个站点的视图,为什么不编写自己的自定义 View Helper 并从视图助手中获取模型中的数据呢?然后,您可以直接从布局中调用此视图帮助器.我认为,只要您不更新/编辑这些数据,就可以从视图中通过模型获取数据完全不会破坏MVC设计模式.

If your partial view is site-wide, why don't you just write your own custom View Helper and fetch the data in your model from your view helper? Then you could call this view helper directly from your layout. In my opinion, fetching data through a model from the view doesn't break the MVC design pattern at all, as long as you don't update/edit these data.

您可以在/view/helpers/或您的库中添加视图助手(然后,您也必须注册视图助手路径):

You can add your view helpers in /view/helpers/ or in your library (then you would have to register your view helper path too):

class Zend_View_Helper_Balance extends Zend_View_Helper_Abstract
{

    public function balance()
    {
        $html = '';
        if (Zend_Auth::getInstance()->hasIdentity()) {
            // pull data from your model
            $html .= ...;
        }
        return $html;
    }
}

请注意,如果您需要以特定方式设置代码格式,则视图助手也可以调用 partial 视图(render()partial()partialLoop()).

Note that you view helper could also call a partial view (render(), partial(), partialLoop()) if you need to format your code in a specific way.

这是一个非常简单的示例,但对我而言,您的情况就足够了.如果您希望对这些数据有更多的控制权,并能够根据特定的视图(或控制器)进行修改(或不进行修改),则建议您查看占位符. Zend在网页上有一个关于它们的很好的例子在线文档.

This is a pretty simple example, but to me it's enough is your case. If you want to have more control on these data and be able to modify it (or not) depending on a particular view (or controller), then I recommend you to take a look on Placeholders. Zend has a really good example about them here on the online documentation.

有关自定义视图助手的更多信息这里.

More information about custom view helpers here.

执行此类任务时,请考虑使用 Zend_Cache 组件,因此您不必在每次请求后都查询数据库,而是每分钟(取决于您的需求).

When you perform such a task, consider using the Zend_Cache component too, so you won't have to query the database after each request but let's say, every minute (depending on your needs).

这篇关于处理每个综合浏览量所需的应用程序范围数据的正确方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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