简体   繁体   中英

Symfony4 generate API rest with crud

With Symfony4 I'm trying to generate a API Rest.

With my existing database and doctrine I generate all the Entity, with make bundle I generate the CRUD controller.

The result is actually in html, I need to manually change the result, for exemple:

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

by

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

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

return $response;

Did I miss something when I generate the CRUD? Does it's possible to generate it directly with Json Response?

If you read the documentation https://symfony.com/doc/current/bundles/SensioGeneratorBundle/commands/generate_doctrine_crud.html it seems that all the available options are:

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

I don't see any option to generate your Controller the way you want, so it might be no, you can't generate it directly with Json Response.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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