简体   繁体   English

Symfony 与 API 平台 + 表格和 controller

[英]Symfony with API Platform + Form and controller

I have started a Symfony 6 project using the classic RegitrationController (that inherits from AbstractController ), RegistrationFormType and User entity obtained from:我已经使用经典的RegitrationController (继承自AbstractController )、 RegistrationFormType和从以下位置获得的User实体启动了 Symfony 6 项目:

symfony console make:registration-form

I can now create a user through the form that adds a new entity in my database.我现在可以通过在我的数据库中添加新实体的表单来创建用户。

I also wanted to create an API using API Plateform .我还想使用API 平台创建一个 API 。 I have then added a couple modifications to my User schema and the API is working, I can create a user through the API too.然后,我对我的User模式添加了一些修改,并且 API 正在工作,我也可以通过 API 创建用户。

I now wonder how to properly couple the form/controller and the API.我现在想知道如何正确耦合表单/控制器和 API。 Indeed, in order not to have redondant code and use the same User schema, I would like to find a way that my RegistrationFormType works with the API.事实上,为了不使用冗余代码并使用相同的User模式,我想找到一种方法让我的RegistrationFormType与 API 一起使用。 Also note that on the one hand I have code in my RegitrationController that, eg, hashes the user password before saving it to the database.另请注意,一方面我的RegitrationController中有代码,例如,在将用户密码保存到数据库之前对其进行哈希处理。 On the other hand this is achieved by a UserDataPersister on the API side.另一方面,这是通过 API 端的UserDataPersister实现的。

What is the good practice to factorize that code and structure my architecture in order to be able to create users from both the form and the API?为了能够从表单和 API 创建用户,分解该代码并构建我的体系结构的良好做法是什么? (I would like to keep the cool things that the Symfony AbstractController provides like createForm , handleRequest , renderForm , etc.) (我想保留 Symfony AbstractController提供的很酷的东西,比如createFormhandleRequestrenderForm等)

I cannot find examples using both Form/Controller and API Plateform .我找不到同时使用 Form/Controller 和API Plateform的示例。

Is it a bad idea to have the Form/Controller and API in the same project?在同一个项目中拥有表单/控制器和 API 是不是一个坏主意?

When it comes to switching to API Platform, you first have to unlearn what you have learned with more traditional apps:)在切换到 API 平台时,您首先必须忘记您在更传统的应用程序中学到的东西:)

Things like creating/rendering forms, and handing requests, are possible, BUT keep in mind that your API accepts, among others, the application/json and not the application/x-www-form-urlencoded .诸如创建/渲染 forms 和处理请求之类的事情是可能的,但请记住,您的 API 除其他外接受application/json而不是application/x-www-form-urlencoded

It all really depends on your choice of client-side code.这完全取决于您选择的客户端代码。 If you stick to more traditional forms with no React , Angular , or Vue , you send the data in application/x-www-form-urlencoded and your API Platform needs a way to denormalize that.如果您坚持使用没有ReactAngularVue的更传统的 forms ,您可以在application/x-www-form-urlencoded中发送数据,并且您的 API 平台需要一种非规范化方式。

There is an official article on that topic: https://api-platform.com/docs/core/form-data/ , but it has its own perks (such as CSRF handling)关于该主题有一篇官方文章: https://api-platform.com/docs/core/form-data/ ,但它有自己的好处(例如 CSRF 处理)

A more common approach is to create a form entirely from scratch using some modern JS framework and just submit the object (in JSON format)更常见的方法是使用一些现代 JS 框架从头开始创建一个表单,然后提交 object(JSON 格式)

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

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