MVC与前端控制器的混乱 [英] MVC with a front controller confusion

查看:108
本文介绍了MVC与前端控制器的混乱的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

**简化问题**

我正在学习oop模式,并且希望构建自己的简单mvc框架.我希望有一个前端控制器,但是我发现很难找到任何可靠的信息来实现使用MVC的前端控制器.

I am learning oop patterns and I am looking to build my own simple mvc framework. I would like this to have a front controller but I am finding it difficult to find any credible information for implementing a front controller with MVC.

尤其是对于前端控制器是应该启动整个三合会还是前端控制器只是简单地调用控制器而其余部分由其余部分来完成感到困惑.

In particular I am confused about whether the front controller should initiate the entire triad or whether the front controller simply calls the controller and the other parts do the rest.

我注意到诸如路由,路由器和引导程序之类的类,我想知道这些特殊的类做什么以及它们是否依赖于前端控制器本身.

I have noticed classes like route, router and bootstrap and I am wondering what these particular classes do and whether they are dependent on the front controller itself.

推荐答案

实际上这不是问题,您只是在尝试构建自己的MVC框架时就如何进行操作提出建议. 因此,我将尝试提供与您的问题一样通用的答案/注意事项.

Actually that's not a question, you're just trying to get suggestions on how to proceed while building your own MVC framework. So I'll try to provide an answer / consideration as generic as your question.

1)我正在学习OOP模式":在错误的手中,模式既强大又危险.我要说的是,您应该开始构建固件,而不要尝试使用网络中遇到的所有模式,这仅仅是因为大型模式正在使用或谈论过这种模式.您可以重构,稍后再编写代码,为每个步骤提供更高的抽象级别:这自然会涉及使用将要阅读的模式以及对它们的更好理解.

1) "I'm learning OOP Patterns": patterns are as much powerful as dangerous in the wrong hands. What I'm trying to say is that you should start building your fw without trying to use every patterns you come across the net just because it is used or talked about by the big ones. You can refactor you code later providing each step an higher level of abstraction: this will naturally involve using the patterns you'll be reading about and a better understanding of them.

2)对于前端控制器是否应该启动整个三合一感到困惑":这取决于您要在mvc中达到的耦合级别. 您可以让Front Controller处理类似以下的所有内容:

2) "confused about whether the front controller should initiate the entire triad": that's up to which level of coupling you're aiming to have in your mvc. You can have your Front Controller handling everything like:

  1. bootstrap:加载配置并实例化数据库连接等
  2. 请求:获取描述所要询问内容的所需数据
  3. 路线:处理请求
  4. 响应:返回询问的内容

但是,如果在其他地方需要配置怎么办?也许在CLI运行脚本中?您自然会从路由器上拆下引导程序组件,以在需要其他任何地方使用它.其他组件也一样.

But what if the configuration is needed somewhere else? Maybe in a CLI running script? You'll be naturally detaching the bootstrap component from the router to use it anywhere else is needed. And the same is for the other components.

3)类,例如路由,路由器和引导程序". 想象一下,让您的大班同学处理所有事情.您将如何测试您的方法?您将使用不同的输入手动调用脚本吗?每种测试方法都必须一次检查输入,路由和输出吗? 为Front Controller中涉及的每个组件提供抽象级别,将其封装在适当的类/对象/模块中,将为您提供更好的测试功能.

3) "classes like route, router and bootstrap". Imagine to have your big class handling everything. How will you be testing your methods? Will you manually call the script with different inputs? Will every testing method have to check for the input, the routing and the output at once? Providing an abstraction level upon every component involved in your Front Controller encapsulating it in a proper class/object/module, will give you far better testing capabilities.

之所以说是因为在创建您正在谈论的内容之前一直走这条路: https://github.com/OverKiller/PHP-Router

I'm talking because I've been down that road before creating exactly what you're talking about: https://github.com/OverKiller/PHP-Router

但是我不得不面对艰苦的测试能力和深层的耦合. 我将很快重写它,抽象出请求,路由和响应组件. 但是我有我的经验,我为此感到骄傲!

But I had to face hard testing capabilities and deep coupling. I'll be rewriting it soon, abstracting the request, the route and the response component. But I had my experience and I'm proud of it!

您应该执行相同的操作. 我要说的是:不要尝试一次构建下一个Ultimate SymZendCakeIgniter PHP框架. 花时间,花时间阅读,花时间学习. 出于上帝的缘故:*即使在不阅读任何有关设计模式的知识之前,也要获得一本关于 T-E-S-T-I-N-G

You should do the same. What I'm trying to say is: do not try to build the next Ultimate SymZendCakeIgniter PHP Framework all at once. Take your time, take your time to read and take your time to learn. And for god sake: *even before reading anything about design patterns get a nice book about T-E-S-T-I-N-G

我希望我有用.

这篇关于MVC与前端控制器的混乱的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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