简体   繁体   English

PHP MVC概念优势

[英]php MVC concept advantages

I have implemented applications using normal php and php classes,Can anyone explain simply the exact concept, difference and advantages of MVC architecture ,I have read many articles about it,But I am looking for a simple answer,Please 我已经使用普通的php和php类实现了应用程序,任何人都可以简单地解释MVC架构的确切概念,区别和优点,我已经阅读了很多有关它的文章,但是我正在寻找一个简单的答案,请

What is the importance of MVC in php? MVC在php中的重要性是什么?

MVC is basically "seperation of concern" MVC基本上是“关注分离”

The MVC pattern separates an application in 3 modules: Model, View and Controller: MVC模式将应用程序分为3个模块:模型,视图和控制器:

The model is responsible to manage the data; 该模型负责管理数据; it stores and retrieves entities used by an application, usually from a database, and contains the logic implemented by the application. 它通常从数据库中存储和检索应用程序使用的实体,并包含应用程序实现的逻辑。

The view (presentation) is responsible to display the data provided by the model in a specific format. 视图 (表示)负责以特定格式显示模型提供的数据。 It has a similar usage with the template modules present in some popular web applications, like wordpress, joomla, … 它与某些流行的Web应用程序(例如wordpress,joomla等)中存在的模板模块具有类似的用法。

The controller handles the model and view layers to work together. 控制器处理模型和视图层以协同工作。 The controller receives a request from the client, invoke the model to perform the requested operations and send the data to the View. 控制器从客户端接收请求,调用模型以执行所请求的操作,并将数据发送到View。 The view format the data to be presented to the user, in a web application as an html output. 视图在Web应用程序中将要呈现给用户的数据格式化为html输出。

Check out: 查看:

- Code and File Organization -代码和文件组织

Just because you created an “/inc” folder and made a “functions.php” file does not mean your code is organized. 仅仅因为您创建了一个“ / inc”文件夹并创建了一个“ functions.php”文件,并不意味着您的代码是有组织的。 When you setup a PHP Framework, it already has a certain folder structure. 设置PHP框架时,它已经具有特定的文件夹结构。 It is expected from you to follow the same standards and keep everything organized in a certain way. 希望您遵循相同的标准,并以某种特定的方式组织一切。 Once you get used to this model, you will never want to go back. 一旦习惯了这种模型,就再也不想回头了。 Unfortunately for some command line champions that still use vi, this can be a challenge. 不幸的是,对于仍然使用vi的某些命令行拥护者来说,这可能是一个挑战。 You will need to work with more files, that are smaller in size. 您将需要处理更多文件,这些文件的大小较小。 But when you use a decent modern code editor or an IDE, it will be a breeze to browse through your application code and find what you need, quickly. 但是,当您使用体面的现代代码编辑器或IDE时,轻而易举地浏览应用程序代码并快速找到所需的内容。

- Utilities and Libraries -公用事业和图书馆

PHP is a great language for web development and provides countless number of tools and libraries. PHP是用于Web开发的出色语言,并提供了无数工具和库。 However, if you ever try to build a whole website with PHP alone, you will find yourself either hunting down a lot of 3rd party code and libraries, or have to write them yourself. 但是,如果您尝试仅使用PHP来构建整个网站,就会发现自己陷入了很多第三方代码和库的麻烦之中,或者不得不自己编写它们。 All top PHP frameworks come with certain Libraries and Helpers, that help you with: Form Validation Input/Output filtering Database Abstraction Session and Cookie Handling Email, Calendar, Pagination etc… The list goes on. 所有顶级PHP框架都附带某些库和帮助程序,可以为您提供帮助:表单验证输入/输出过滤数据库抽象会话和Cookie处理电子邮件,日历,分页等…清单还在继续。 Not to mention, there is plenty of plugins provided by the community that you can add to your framework. 更不用说,社区提供的大量插件可以添加到框架中。

- Security -安全

In PHP you can already find many input and output filtering functions to protect your website against certain attacks. 在PHP中,您已经可以找到许多输入和输出过滤功能,以保护您的网站免遭某些攻击。 However, manually using these functions can get tiring and you may forget about them every once in a while. 但是,手动使用这些功能会很累,您可能会不时地忘记它们。 With a framework, most of the work can be done for you automatically. 使用框架,大多数工作可以自动为您完成。 For example in CodeIgniter: 例如在CodeIgniter中:

  • Any value passed to database object gets filtered against SQL injection attacks. 传递给数据库对象的任何值都将针对SQL注入攻击进行过滤。
  • All html generating functions, such as form helpers and url helpers filter the output automatically. 所有的html生成功能(例如表单帮助程序和url帮助程序)都会自动过滤输出。
  • All user input can be filtered against XSS attacks. 可以针对XSS攻击过滤所有用户输入。
  • Encrypting cookies automatically is only a matter of changing a config option. 自动加密cookie只是更改config选项的问题。

-Less Code & Faster Development -更少的代码和更快的开发

There is of course a learning curve for all PHP Frameworks. 当然,所有PHP框架都有一条学习曲线。 But once you get over this hump, you will enjoy the benefits of rapid application development. 但是一旦克服了这一难题,您将享受快速应用程序开发的好处。

You will write less code, which means less time spent typing. 您将编写更少的代码,这意味着更少的输入时间。 You will not have to chase down 3rd party libraries all the time for every new project because most of them will come with the default framework install. 您不必为每个新项目都一直追逐第三方库,因为大多数第三方库都带有默认框架安装。

Also, since you are being more organized, it will be much faster to chase down bugs, maintain code, and make changes to existing code. 另外,由于您的组织更加井井有条,因此查找错误,维护代码和更改现有代码的速度将更快。

-Performance Tools -性能工具

One of the main arguments from the naysayers comes in this subject. 反对者的主要论据之一就是这个主题。 There is obviously a performance hit when you build a “Hello World” application with a framework vs. plain PHP code. 当您使用框架对普通的PHP代码构建“ Hello World”应用程序时,显然会降低性能。

But those benchmarks are just bad examples. 但是这些基准只是不好的例子。 First of all, you should understand that developers are more expensive than servers. 首先,您应该了解开发人员比服务器昂贵。 Saving time from development and maintenance is likely to outweigh any extra money you need to spend on servers. 从开发和维护中节省时间可能超过您需要花在服务器上的任何额外资金。

Putting all of that aside, you can actually gain performance benefits by using a PHP framework. 撇开所有这些,您实际上可以通过使用PHP框架获得性能上的好处。 They come with tools that help you do caching, benchmarks, profiling etc… 它们带有可帮助您进行缓存,基准测试,性能分析等的工具。

Modern frameworks are also great with dynamic loading, as they load code only as needed. 现代框架也适合动态加载,因为它们仅根据需要加载代码。 Different page requests can load different amount of library code based on what needs to be used. 不同的页面请求可以根据需要使用的内容来加载不同数量的库代码。

-Suitable for Teamwork -适合团队合作

The way your project is organized in a PHP Framework also helps you create a suitable environment for teamwork. 在PHP框架中组织项目的方式还可以帮助您创建适合团队合作的环境。

You can let your designers work on the Views, database guru work on the Models, let the smart programmer (yourself ;) ) build reusable Libraries and Plugins etc… 您可以让您的设计师在Views上工作,数据库专家在Models上工作,让聪明的程序员(您自己;))构建可重用的库和插件等。

Also you can let someone build unit tests, because they come with tools for that too. 您也可以让某人构建单元测试,因为他们也提供用于该单元测试的工具。

And It's Fun! 而且很有趣!

In MVC your model, your view and your controller are differents parts. 在MVC中,您的模型,视图和控制器是不同的部分。

So you can change your view without changing your controller and your model. 因此,您可以更改视图而无需更改控制器和模型。

For example : you have all the informations you wan't in your site, you jsut wan't to change the design. 例如:您拥有站点中所有不需要的信息,而您不想更改设计。 You don't have to do any changes in the model code, change change the view aprt. 您无需在模型代码中进行任何更改,只需更改即可更改视图。

That's the same idea in php templates, but works for the three part. 这与php模板中的想法相同,但适用于这三部分。

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

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