PHP MVC-Controller和View如何访问相同的Model实例? [英] PHP MVC - How can Controller and View access same instance of Model?

查看:173
本文介绍了PHP MVC-Controller和View如何访问相同的Model实例?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为数据库应用程序开发我自己的PHP MVC,我已经完成了,但是使用了可完成所有工作的单片类-MySQL处理,输入处理和显示.它的工作原理很棒,但是调试和进行更改是一场噩梦!我最近发现了MVC,并且正在重写应用程序-目前所有内容都在绘图板上,因此我实际上没有可与您共享的任何代码.

I'm developing my own PHP MVC for a database application, which I'd already completed but using monolithic classes that do everything - MySQL processing, input processing and display. It works great but it's a nightmare to debug and to make alterations! I've recently discovered MVC's and in the process of rewriting the app - it's all on the drawing board at the moment so I haven't actually got any code to share with you.

我m绊绊的一件事是:控制器应该调用Model以根据用户输入对其进行修改,然后调用View,该View将访问需要获取的Model它的数据,然后显示它.我希望视图独立于Controller来获取其数据,而不希望Controller充当中介.我从Controller的那一行开始,从Model提取数据并将其交给View,但是很快就遇到麻烦,因为View需要访问多个Model.

There's one thing I'm stumbling on: I've got it that the Controller should call the Model to modify it based on user input, and then call the View, which will access the Model(s) it needs to get its data and then display it. I'd like the view to gets its data independently of the Controller, and not have the Controller act as the intermediary. I started off down that line of the Controller getting its data from the Model and handing it to the View, but quickly ran into trouble where a View would need access to several Models.

为解释我的问题,我举一个例子:

To explain what I'm stuck on, I'll draw an example:

示例:提交包含数据的表单,该表单无法通过模型验证

前端控制器:

  • 加载相关控制器并调用ProcessForm操作
  • Load the relevant controller and call the ProcessForm action

控制器:

  • 实例化Model类并调用方法以加载和验证数据
  • Instantiate the Model class and call the methods to load and validate the data

型号:

  • 验证失败:生成错误列表并返回'false'
  • validation failed: generate a list of errors and return 'false'

控制器:

  • 如果模型已返回"true",则重定向到索引"页面
  • 返回"false":调用视图
  • If Model had returned 'true', redirect to the Index page
  • It returned 'false': invoke the View

查看:

  • 实例化Model类并获取数据
  • 显示数据和错误消息
  • Instantiate the Model class and fetch the data
  • Display the data and the error message(s)

我的问题是,拥有自己的Model实例的View如何获得在Controller的Model实例中生成的错误消息,而无需Controller将其自己的Model实例直接传递给看法?我在想第一个实例需要将消息存储在第二个实例可以检索它们的地方?

My problem is, how can the View, which has its own instance of the Model, get the error message(s) which were generated in the Controller's instance of the model, without the Controller handing its own Model instance directly to the View? I'm figuring the first instance would need to store the messages somewhere where the second instance could retrieve them?

我意识到第一个实例可以将其错误返回给Controller,然后可以将其传递给视图,而与Model本身无关,但是我仍然希望保持View独立于Controller.

I realise the first instance could return its errors to the Controller, which it can then pass to the view, independent of the Model itself, but I'd still like to keep the View independent of the Controller.

推荐答案

好吧,拳头,根本没有模型".在MVC和受MVC启发的设计模式中的模型是,其中包含多种结构,就像表示层包含控制器,视图和其他一些东西.

Well .. fists of all, there are no "models". Model in MVC and MVC-inspired design patterns is a layer, that contains multitude of structures, just like presentation layer contains controllers, views and some other things.

除了那一部分之外,您的问题是,您具有这些模型"的单独实例.相反,控制器和视图应该共享同一工厂,该工厂可以产生并缓存实例.

That part aside, your issue is that you are having separate instances of those "models". Instead both controllers and views should share same factory, which can produce and cache the instances.

最佳解决方案是为您提供服务,这些服务代表模型层的各个部分,并且控制器和视图通过这些服务与模型进行交互.工厂会在请求时首次初始化服务,然后在重复请求时提供相同的实例.

The optimal solution would be for you to have services, which represent parts of model layer and through which controllers and views interact with model. A factory would initialize the service first time when it is requested, and then provide the same instance on any repeated request.

class ServiceFactory
{
    private $cache = array();

    public function create( $name )
    {
        if ( array_key_exists($name, $this->cache) === false )
        {
            $this->cache[$name] = new $name;
        }
        return $this->cache[$name];
    }
}

这是服务工厂的非常精简" 版本.

This is the "extremely dumbed down" version of service factory.

在引导阶段,您初始化服务工厂,然后将其注入实例实例的构造函数中,从而为当前控制器和当前视图提供实例.

At the bootstrap stage you initialize the service factory and then provide both the current controller and current view with instance of it, by injecting it in the constructors of said instances.

这篇关于PHP MVC-Controller和View如何访问相同的Model实例?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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