简体   繁体   English

Symfony MVC 控制器最佳实践

[英]Symfony MVC best practice for controllers

In a Symfony/Doctrine/PHP-project, a client is complaining that we've broken software development best practices.在 Symfony/Doctrine/PHP 项目中,客户抱怨我们违反了软件开发最佳实践。 The complaint is about improper layering of the source code, and the lack of unit tests.抱怨是关于源代码分层不当,以及缺乏单元测试。

  • This is a sub $50k-project.这是一个低于 5 万美元的项目。
  • I believe that the client has an expert from the Java world, perhaps Spring Framework, looking at the source code.相信客户端有Java界的高手,可能是Spring Framework,看源码。

We've been using proper MVC, as we see it.正如我们所见,我们一直在使用适当的 MVC。

  • View-logic is handled entirely by TWIG.视图逻辑完全由 TWIG 处理。
  • Database is handled entirely by Doctrine.数据库完全由 Doctrine 处理。
  • We're using Symfony Security for access control ($this->get('security.context')->isGranted('ROLE_ADMIN') and $this->get('security.context')->getToken()->getUser() .我们使用 Symfony Security 进行访问控制($this->get('security.context')->isGranted('ROLE_ADMIN')$this->get('security.context')->getToken()->getUser()

Beware that Symfony has changed the model a bit, since we started this project - but remains backward compatible.请注意,自从我们开始这个项目以来,Symfony 已经稍微改变了模型——但仍然向后兼容。

In the controller the customer is specifically saying that it's wrong for the controller to handle:在控制器中,客户专门说控制器处理是错误的:

  • Access Control (via Symfony Security)访问控制(通过 Symfony Security)
  • Database Queries (via Doctrine)数据库查询(通过 Doctrine)
  • "Parsing and other logic" for sending back responses ( return $this->render('some_template.html.twig'); )用于发回响应的“解析和其他逻辑”( return $this->render('some_template.html.twig');

The question问题

The client is saying that best practices is for the controller to simply pass on requests to another layer further down in the system .客户说最好的做法是让控制器简单地将请求传递到系统中更远的另一层

Further he's saying that user-admin is based on a "custom model" where all users and roles are stored in the database - which makes plugging in a different access control system difficult.此外,他说用户管理基于“自定义模型”,其中所有用户和角色都存储在数据库中 - 这使得插入不同的访问控制系统变得困难。 Specifically because role names seems to be hard coded such as via commands such as ($this->get('security.context')->isGranted('ROLE_ADMIN') .特别是因为角色名称似乎是硬编码的,例如通过诸如($this->get('security.context')->isGranted('ROLE_ADMIN')

So;所以; is there a definitive best practice on this field?这个领域有明确的最佳实践吗? What belongs in the controller, and is Doctrine, Twig, Symfony Security "sufficiently" a separate layer "below the controller".属于控制器的东西,是 Doctrine、Twig、Symfony Security,“足够”是“控制器下方”的一个单独层。

Should there be yet another layer between the controller and Doctrine for example?例如,控制器和 Doctrine 之间是否应该还有另一层?

Source: http://fabien.potencier.org/what-is-symfony2.html来源: http : //fabien.potencier.org/what-is-symfony2.html

First, what is Symfony2?首先,什么是Symfony2?

First, Symfony2 is a reusable set of standalone, decoupled, and cohesive PHP components that solve common web development problems.首先,Symfony2 是一组可重用的独立、解耦和内聚的 PHP 组件,用于解决常见的 Web 开发问题。

Then, based on these components, Symfony2 is also a full-stack web framework.那么,基于这些组件,Symfony2 也是一个全栈的 web 框架。

Depending on your project and depending on your needs, you can either pick and choose some of the Symfony2 components and start your project with them, or you can use the full-stack framework and benefit from the tight integration it provides out of the box.根据您的项目和您的需要,您可以选择一些 Symfony2 组件并使用它们开始您的项目,或者您可以使用全栈框架并从它提供的开箱即用的紧密集成中受益。 And choosing between the two different approaches is really up to you.在两种不同的方法之间进行选择完全取决于您。

Is Symfony2 an MVC framework? Symfony2 是一个 MVC 框架吗?

Symfony2 is really about providing the tools for the Controller part, the View part, but not the Model part. Symfony2 实际上是为控制器部分、视图部分提供工具,而不是为模型部分提供工具。 It's up to you to create your model by hand or use any other tool, like an ORM.您可以手动创建模型或使用任何其他工具(如 ORM)。 Of course, tight integration exists for the most well known ORMs like Doctrine2 and Propel;当然,对于 Doctrine2 和 Propel 等最著名的 ORM 来说,紧密集成是存在的。 but they are optional dependencies.但它们是可选的依赖项。 The Symfony2 core features do not and will never rely on any ORM. Symfony2 的核心特性不会也永远不会依赖于任何 ORM。

Symfony2 is an HTTP framework; Symfony2 是一个 HTTP 框架; it is a Request/Response framework.它是一个请求/响应框架。 That's the big deal.这就是大事。 The fundamental principles of Symfony2 are centered around the HTTP specification. Symfony2 的基本原则以 HTTP 规范为中心。

Symfony about best practices: https://symfony.com/doc/current/best_practices.html关于最佳实践的 Symfony: https : //symfony.com/doc/current/best_practices.html

You should rly read this about controller best practices:https://symfony.com/doc/current/best_practices.html#controllers您应该阅读有关控制器最佳实践的内容:https ://symfony.com/doc/current/best_practices.html#controllers

You can read this answer https://stackoverflow.com/a/21701890/2160958您可以阅读此答案https://stackoverflow.com/a/21701890/2160958

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

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