PHP 中的 MVC – 一般理解和具体问题 [英] MVC in PHP – General understanding and specific questions

查看:51
本文介绍了PHP 中的 MVC – 一般理解和具体问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在尝试了解 MVC 框架如何在 PHP 中工作.因此,我创建了这个我如何认为在 PHP 中实现 MVC 的基本草图:

[我知道缺少某些步骤,例如Router 如何解析 route 以便知道要加载什么 ViewController,但这些步骤相当以技术为基础,对于理解 PHP 中 MVC 的一般流程并不重要.]

我从这个.

  • 一般有两种方法,要么你的Controllers和Views的比例是1:1,然后在Controller完成后,你的bootstrap脚本调用同名的View(LoginController,LoginView),或者你的Controller返回视图名称以及操作和参数,由引导程序调用.然后视图选择一个模板,该模板可以包含其他子模板(如用户栏或页脚).
  • 在这种情况下,您的视图需要能够根据 Accept: HTTP 标头选择不同的模板(并发送类似 Accept: application/json在您的 AJAX 请求中).然后,您的视图将返回 JSON 模板而不是 HTML 模板.
  • <小时>

    你的草图有什么问题?

    您的模型不仅仅是数据库的网关,它还是所有逻辑发生的地方.所有的计算.看到另一个很好的答案,它解释了 模型应该如何在 MVC 中构建?.

    MVC 的思想是简单地将您的应用程序分成三层:输入(控制器)、逻辑(模型)和输出(视图).这是对 PHP 工作方式的扩展(这是一个请求,给我一个响应,都在同一页面中).

    因此,实现细节可能会有所不同,概念才是最重要的.Web MVC"仅仅是良好的 OOP 实践和几十年前某人制定的一些命名约定的结果.

    I am currently trying to understand how the MVC framework does work in PHP. Therefore, I have created this basic sketch of how I think that MVC is implemented in PHP:

    [I know that some steps are missing, e.g. how the Routerparses the route in order to know what View and Controller to load, but these steps are rather technical based and are not important in order to understand the general flow of MVC in PHP.]

    I draw my understanding of MVC in PHP from this article series. However, I think that this structure will differ from most of the structures people think off, when they talk about MVC in PHP, because of this article (The article basically states that not only the Controller but also the View does communicate with the Model).

    What I'd like to ask you now are several questions:

    1. Is this generally a right way of implementing MVC in PHP?
    2. How can I add a Login/Logout-System to this approach? So that user, who are not logged in, will always see the LoginView and users who are logged in can see different views? (I know how a login system does work, but I can't figure out where to place it in the MVC, so I don't have to insert the code multiple times, e.g. in each Controller.)
    3. What if my application consist of multiple elements (say a user bar [with user name, link to settings, etc.], a navigation and a content container) that are always loaded? How can I assemble these elements to a final view? (The only idea that comes to my mind is to assemble the final view in each view separately, but that would mean that I have to insert the code for it multiple times in each view, which misses the point of MVC, doesn't it?)
    4. What if I want to use AJAX in my application? My idea would be to send ajax requests through the framework as well, while only accessing controllers and views that are made for ajax? In other words the AjaxViews do only return e.g. json objects and the AjaxControllers do always expect authentication codes to prove that these ajax calls are legtitim?

    I know that there have already been asked dozens of questions about MVC in PHP and I've been reading quiet a lot of articles until now, but I think that only reading does not enable me to understand MVC completely.

    Furthermore, after reading the articles linked above, I'm not longer sure whether other articles about MVC, I find on the web, does explain MVC the same way as the articles above. Because if they don't, I'm trying to understand one framework while reading about two or multiple different approaches.

    Thank you very much in advance for taking the time to answer my question!

    -- --- --- Update --- --- --

    According to the answer below I've changed my MVC sketch. Just in case someone finds this link and wants to know more.

    解决方案

    Let me first answer your questions, then place my take on it.

    1. There's no right way of writing MVC. There are so many flavors and variations, and that get even multiplied when talking about web MVC.
    2. About Logging in and Logging out. I think the most robust system would be a Role Based Access Control combined with an Access Control List, see How can I implement an Access Control List in my Web MVC application?.
    3. There are generally two approaches, either you have a 1:1 ratio between Controllers and Views, and then after the Controller is done, your bootstrap script calls the View with the same name (LoginController, LoginView), or your Controller returns the View name along with action and parameters, to be called by the bootstrapper. The view then picks a template, and that template can include other sub-templates (like the user bar, or the footer).
    4. In that case, your view needs to have an ability to select a different template based on the Accept: HTTP header (and send something like Accept: application/json in your AJAX requests). Your view then returns a JSON template instead of an HTML template.


    What is wrong with your sketch?

    Your model isn't just the gateway to your database, it's where all the logic happens. All the computation. See this yet another excellent answer that explains How should a model be structured in MVC?.

    The idea of MVC is to simply separate your application into three layers: Input (controller), Logic (model) and Output (view). This is to extend on the usual way PHP works (here's a request, give me a response, all in the same page).

    For that reason, implementation details may vary, the concept is what matters. "Web MVC" is merely the result of good OOP practices and some naming convention someone made up a few decades ago.

    这篇关于PHP 中的 MVC – 一般理解和具体问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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