为什么要使用 PHP 框架?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1851920/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Why use a framework with PHP?
提问by ste
I'm curious, what are the advantages (and disadvantages) on using a framework with PHP?
我很好奇,在 PHP 中使用框架有什么优点(和缺点)?
I've been using PHP on and off since version 3. I've never used any of the frameworks available for PHP, so what am I missing out on?
我从第 3 版开始就断断续续地使用 PHP。我从未使用过任何可用于 PHP 的框架,那么我错过了什么?
采纳答案by Graviton
Framework abstracts you from low level details, makes you more productive, and protects you from low level error ( such as preventing SQL injection attacks).
框架将您从低级细节中抽象出来,提高您的工作效率,并保护您免受低级错误(例如防止 SQL 注入攻击)的影响。
A good PHP framework forces you to separate your concern and implements the proven architecture, which in the end improves your design, and makes your code much easier to read and maintain and unit test.
一个好的 PHP 框架会迫使您将关注点分开并实现经过验证的架构,这最终会改进您的设计,并使您的代码更易于阅读、维护和单元测试。
Here are somereferences on why you shoulduse framework
回答by Jon Winstanley
Why use a Framework?
为什么要使用框架?
(I am a Symfonydeveloper and so I will answer in a way revolving around Symfony. There are other frameworks that have similar functionality, however, in my opinion none are quite as comprehensive or well written as Symfony).
(我是一名Symfony开发人员,所以我将以围绕Symfony的方式回答。还有其他具有类似功能的框架,但是,在我看来,没有一个框架像Symfony那样全面或写得很好)。
Quoted from the Symfony Web site:
引用自 Symfony 网站:
A frameworkstreamlines application development by automating many of the patterns employed for a given purpose. A framework also adds structure to the code, prompting the developer to write better, more readable, and more maintainable code. Ultimately, a framework makes programming easier, since it packages complex operations into simple statements.
Symfony is a complete framework designed to optimize the development of web applicationsby way of several key features. For starters, it separates a web application's business rules, server logic, and presentation views. It contains numerous tools and classesaimed at shortening the development time of a complex web application. Additionally, it automates common tasksso that the developer can focus entirely on the specifics of an application. The end result of these advantages means there is no need to reinvent the wheel every time a new web application is built!
Symfony is written entirely in PHP 5. It has been thoroughly testedin various real-world projects, and is actually in use for high-demand e-business websites. It is compatible with most of the available databases engines, including MySQL, PostgreSQL, Oracle, and Microsoft SQL Server. It runs on *nix and Windows platforms. Let's begin with a closer look at its features.
一个框架,通过自动化许多针对特定目的所采用的模式简化应用开发。框架还为代码添加了结构,促使开发人员编写更好、更具可读性和更易于维护的代码。最终,框架使编程更容易,因为它将复杂的操作打包成简单的语句。
Symfony 是一个完整的框架,旨在通过几个关键功能优化 Web 应用程序的开发。首先,它将 Web 应用程序的业务规则、服务器逻辑和表示视图分开。它包含许多旨在缩短复杂 Web 应用程序开发时间的工具和类。此外,它还可以自动执行常见任务,以便开发人员可以完全专注于应用程序的细节。这些优势的最终结果意味着每次构建新的 Web 应用程序时都无需重新发明轮子!
Symfony完全是用 PHP 5 编写的。它已经 在各种实际项目中进行了彻底的测试,并且实际上用于高需求的电子商务网站。它与大多数可用的数据库引擎兼容,包括 MySQL、PostgreSQL、Oracle 和 Microsoft SQL Server。它在 *nix 和 Windows 平台上运行。让我们先仔细看看它的特点。
Symfony's Features - I.e. What you are missing out on...
Symfony 的功能 - 即你错过了什么......
Most of the common features of web projects are automated within symfony, as follows:
Web 项目的大部分常见功能都在 symfony 中自动化了,如下所示:
- The built-in internationalizationlayer allows for both data and interface translation, as well as content localization.
- The presentation uses templates and layoutsthat can be built by HTML designers without any knowledge of the framework. Helpers reduce the amount of presentation code to write by encapsulating large portions of code in simple function calls.
- Forms supportautomated validation and repopulation, and this ensures a good quality of data in the database and a better user experience.
- Output escapingprotects applications from attacks via corrupted data.
- The cache managementfeatures reduce bandwidth usage and server load.
- Authentication and credential featuresfacilitate the creation of restricted sections and user security management.
- Routing and smart URLsmake the page address part of the interface and search-engine friendly.
- Built-in e-mail and API managementfeatures allow web applications to go beyond the classic browser interactions.
- Listsare more user-friendly thanks to automated pagination, sorting, and filtering.
- Factories, plug-ins, and mixinsprovide a high level of extensibility.
- Ajaxinteractions are easy to implement thanks to one-line helpers that encapsulate cross-browser-compatible JavaScript effects.
- The built-in unit and functional testingframework provides the perfect tools to allow test-driven development.
- The debug panelaccelerates debugging by displaying all the information the developer needs on the page he's working on.
- The command-line interface to automate application deployment between two servers.
- The loggingfeatures give administrators full details about an application's activities.
- 内置的国际化层允许数据和界面翻译,以及内容本地化。
- 演示文稿使用的模板和布局可以由 HTML 设计人员构建,无需任何框架知识。帮助程序通过将大部分代码封装在简单的函数调用中来减少要编写的表示代码量。
- 表单支持自动验证和重新填充,这确保了数据库中数据的良好质量和更好的用户体验。
- 输出转义保护应用程序免受损坏数据的攻击。
- 该高速缓存管理功能减少了带宽使用和服务器负载。
- 身份验证和凭据功能有助于创建受限部分和用户安全管理。
- 路由和智能 URL使页面地址成为界面和搜索引擎友好的一部分。
- 内置的电子邮件和 API 管理功能允许 Web 应用程序超越经典的浏览器交互。
- 由于自动分页、排序和过滤,列表更加用户友好。
- 工厂、插件和mixin提供了高度的可扩展性。
- 由于封装了跨浏览器兼容的 JavaScript 效果的单行助手,Ajax交互很容易实现。
- 在内置的单元测试和功能测试框架提供了完美的工具,使测试驱动开发。
- 在调试面板加速通过显示的所有信息,他的工作对页面上的开发者需要调试。
- 用于在两台服务器之间自动部署应用程序的命令行界面。
- 该日志记录功能让管理员对应用程序的活动的全部细节。
回答by David Espart
Do not reinvent the wheel
不要重新发明轮子
That's what is all about. A framework have all the main functionality implemented using the best practices acknowledged through years of experience.
这就是全部。一个框架使用多年经验公认的最佳实践实现了所有主要功能。
回答by lukas.pukenis
Well maybe because you can use C++ instead of PHP in the first case :).
好吧,也许是因为在第一种情况下您可以使用 C++ 而不是 PHP :)。
A framework is used in order to achieve better goals in terms of quality, securityetc. in less time using defined structures and implementations.
使用框架以使用定义的结构和实现在更短的时间内在质量、安全性等方面实现更好的目标。
If you are not using already made framework, then you're making your own but still a framework.
如果您没有使用已经制作的框架,那么您正在制作自己的但仍然是一个框架。
回答by Alex Dev
I guess frameworks are designed to speed up development of web services and admin sites. I use backvendor Yii extension and it really helps me to save time on the first stages.
我猜框架旨在加速 Web 服务和管理站点的开发。我使用后端供应商 Yii 扩展,它确实帮助我节省了第一阶段的时间。
回答by sylv3rblade
Speed of development for one. It also removes a lot of burdens that come with coding stuff manually (and yes it means don't reinvent the wheel).
发展速度为一。它还消除了手动编码带来的许多负担(是的,这意味着不要重新发明轮子)。
回答by johannes
A good framework guides you through best practices for your architecture and gives you somee often needed functionality in a proven and stable way. So you don't have to reinvent the wheel all the time.
一个好的框架会指导您完成架构的最佳实践,并以经过验证且稳定的方式为您提供一些经常需要的功能。所以你不必一直重新发明轮子。

