【问题标题】:Symfony4 generate API rest with crudSymfony4 使用 crud 生成 API 休息
【发布时间】:2021-03-13 06:52:03
【问题描述】:

我正在尝试使用 Symfony4 生成 API Rest。

使用我现有的数据库和学说,我生成所有实体,使用 make bundle 我生成 CRUD 控制器。

结果实际上是html,我需要手动更改结果,例如:

return $this->render('user/show.html.twig', [
            'user' => $user,
        ]);

通过

$data =  $this->get('serializer')->serialize($user, 'json');

$response = new Response($data);
$response->headers->set('Content-Type', 'application/json');

return $response;

我在生成 CRUD 时是否遗漏了什么?是否可以直接用 Json Response 生成?

【问题讨论】:

    标签: json rest symfony


    【解决方案1】:

    如果您阅读文档https://symfony.com/doc/current/bundles/SensioGeneratorBundle/commands/generate_doctrine_crud.html,似乎所有可用选项都是:

    • --entity
    • --route-prefix
    • --with-write
    • --format
    • --overwrite

    我没有看到以您想要的方式生成控制器的任何选项,所以它可能不是,您不能直接使用 Json Response 生成它。

    【讨论】:

    • 我不使用 SensioGeneratorBundle,因为它已被弃用,我问这个问题是因为可能是其他捆绑包允许我这样做
    猜你喜欢
    • 2018-02-28
    • 1970-01-01
    • 2014-06-16
    • 2014-12-10
    • 2018-10-20
    • 2015-09-26
    • 2020-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多