简体   繁体   English

MVC概念是否适用于静态PHP页面?

[英]Does the MVC concept apply to static PHP pages?

I am trying to build my own PHP MVC framework. 我正在尝试构建自己的PHP MVC框架。

At the moment I am creating a simple 2 page website where each page extends a template page so that I don't have to worry about the meta tags, header, navigation and footer when creating new pages. 目前,我正在创建一个简单的2页网站,其中每个页面都扩展了模板页面,这样我在创建新页面时不必担心meta标签,页眉,导航和页脚。

I am trying to figure out a way to spilt this into the separate components of MVC but can't 我试图找出一种方法可以将其传播到MVC的单独组件中,但不能

This leads me to believe that MVC isn't necessarily the way forward with this framework. 这使我相信MVC不一定是此框架的前进方向。

So, does the MVC concept apply to static(by static I mean the only user input is clicking on links, no logging in or anything) PHP pages? 那么,MVC概念是否适用于静态(通过静态,我的意思是唯一的用户输入是单击链接,没有登录等)PHP页面?

Do you need MVC 您需要MVC吗

  • Can the MVC concept apply to a website with static pages? MVC概念可以应用于具有静态页面的网站吗? Yes , it can. 是的 ,可以。
  • Should you do use it? 你应该使用它吗? No , unless you expect the project to grow. ,除非您期望该项目能够发展。

MVC architecture, at the core of it, is about separating the business logic from the presentational logic. MVC体系结构的核心是关于将业务逻辑与表示逻辑分离。

In (what you call) static site, the only role of model layer (yes, it is a layer, not a class) would be to fetch content from either cache or database. 在(称为)静态站点中,模型层(是,它是一个层,而不是一个类)的唯一作用是从缓存或数据库中获取内容。 In this case you will get more frame then work , because setting up a MVC structure would take more code, then the useful parts. 在这种情况下,您将获得更多的框架,然后再工作 ,因为建立MVC结构将花费更多的代码,然后是有用的部分。

Should you build a framework 你应该建立一个框架吗

Definitely make one. 绝对可以。 In PHP community this almost like a rite of passage. 在PHP社区中,这几乎像是通过仪式。 If you have not made at least few frameworks, you are not a real PHP developer. 如果您至少没有建立过几个框架,那么您不是一个真正的PHP开发人员。

Though, you should keep in mind, that it will take at least 3 iterations for you to build something that does not suck. 但是,请记住,构建不吸引人的东西至少需要3次迭代。 And that is assuming that you actually study OOP principles instead of mimicking some other framework. 那就是假设您实际上是在研究OOP原理,而不是模仿其他框架。

What would be a better options for a tiny project 什么是小型项目的更好选择

Instead of writing a full MVC framework, try to write something that follows the spirit of it. 与其编写一个完整的MVC框架,不如尝试遵循其精神。

There are two things you should start from: 您应该从两件事开始:

  1. you application should have Single Point of Entrance . 您的应用程序应具有“ 单一入口” This would mean, if user writes http://who.cares/article/1 , then you should rewrite the URL to index.php?url=article/1 这意味着,如果用户写了http://who.cares/article/1 ,那么您应该将URL重写为index.php?url=article/1

  2. separate the concerns. 分离关注点。 Basically you have 3 different thins to do in such applications: 基本上,在这些应用程序中您可以做3种不同的设置:

    • parse the user's request 解析用户的请求
    • fetch the data 获取数据
    • display the collected information 显示收集的信息

For the last part you might benefit from looking at this article . 对于最后一部分,您可能会受益于阅读本文 For the rest, you will have to decide on your own. 其余的,您将不得不自己决定。

Just don't the ancient mysql_* functions for collecting data from database (if you have any). 只是不要使用古老的mysql_*函数从数据库中收集数据(如果有的话)。 They are no longer maintained and community has begun the deprecation process . 它们已不再维护,社区已开始弃用过程 Instead you should learn about prepared statements and use either PDO or MySQLi . 相反,您应该了解准备好的语句,并使用PDOMySQLi If you cannot decide, this article will help to choose. 如果您不能决定, 本文将有助于选择。 If you care to learn, here is a quite good PDO-related tutorial . 如果您想学习, 这里有一个很好的PDO相关教程

Since your pages need a programing language to change the content , do something with the user input etc , it's not that static at all. 由于您的页面需要使用编程语言来更改内容,使用用户输入进行某些操作等,所以这并不是完全静态的。 They are static when u use only markup and styling languages. 当您仅使用标记和样式语言时,它们是静态的。

It's hard to say if ur project needs a solid architecture. 很难说您的项目是否需要可靠的架构。 For static pages , you don't need mvc. 对于静态页面,您不需要mvc。 For your 2 page project again i think you don't need mvc unless your pages are very dynamic ( 1 page can have several states) . 再次对于您的2页项目,我认为您不需要mvc,除非您的页面非常动态(1页可以有几种状态)。

You should read more about the structure, achitecture of an application if you really want to make your own framework. 如果您真的想创建自己的框架,则应该阅读有关应用程序的结构和架构的更多信息。 Usually only medium/big applications need a solid structure/architecture (ex. : mvc). 通常,只有中型/大型应用程序需要坚实的结构/体系结构(例如:mvc)。

Final answer : i don't think your app needs mvc . 最终答案:我认为您的应用程序不需要mvc。
PS : It's good to make your own mvc framework , you learn alot of things . PS:创建自己的mvc框架是一件好事,您可以学到很多东西。 But it will take alot to finalise it if your doing it alone (if u want to make it better than the existing ones ). 但是,如果您一个人做(如果您想使其比现有的更好),则需要花很多时间才能完成。 There is no solid (nearly perfect :) ) php mvc framework outhere. 没有坚实的(近乎完美的:))php mvc框架。 Php community lacks a good and world wide accepted framework. php社区缺乏一个良好的,全球公认的框架。 Many frameworks but all have theyr pros and cons. 许多框架,但都有其优点和缺点。 I even heared some devs saying zend mvc is more of a library . 我什至听说有些开发人员说zend mvc更像是一个库。

First of all, mvc is NEVER necessary. 首先,不需要mvc。 This is just a pattern recognized to be handy providing organization, robusty, safety... 这只是一种公认​​的方便模式,可提供组织,鲁棒性,安全性...

Then, if you want my opinion the earlier you get to be familiar with it implementing it in your static pages the easier it is going to be to go ahead with your next pages. 然后,如果您希望我的意见早熟,那么您应该在静态页面中实现它,就越容易进行下一页。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM