PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Thursday, May 12, 2022

[FIXED] How to bind a custom route to a method of a controller via yaml without triggering an automatic query from api-platform and symfony?

 May 12, 2022     api-platform.com, controller, php, symfony     No comments   

Issue

  • I have a simple symfony project base on api-platform.
  • I have an entity named "MyEntity".
  • I have a ressource yaml config file to tell api-platform how my entity to be query via api call .
  • In the yaml, I added a route named exportcsv exposed as export, it will be called by my front with this url : http://127.0.0.1:8000/api/myentitys/export.
  • This route is mapped to call the export method from MyEntity controller.
  • In MyEntity controller I have a method named export and I will do nothing except dumping a sentence then die ( dd('why?!'); ).

Expected behavior:

  • call the export url
  • Nothing should be done on the server/database, and the front will just receive a dump of the string why?!

Actual behavior:

  • call the export url
  • execute a query on the table'db named myentity
  • then receive a dump of the string why?!

I discovered the query when I added data to my table. Performance went longer and longer as I added more data on the table. I would never reach the Why?! at some point. I checked my database, and saw that a select all on the myentity table were active. I searched a bit on the documentation, the only thing I could find is :

pagination_partial: true

When adding this in the yaml, to the export route, it will still execute a query, but as it now paginated, it will take way less time.


My question is:

How to totatlly remove this query at all ?


In Controller/MyEntityController.php :

        namespace App\Controller;
        
        use App\Entity\MyEntity;
        use Doctrine\ORM\EntityManagerInterface;
        use Exception;
        // use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
        use Symfony\Component\Translation\Exception\NotFoundResourceException;
        use Symfony\Bundle\FrameworkBundle\Console\Application;
        use Symfony\Component\Console\Input\ArrayInput;
        use Symfony\Component\Console\Output\NullOutput;
        use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;
        use Symfony\Component\HttpFoundation\File\UploadedFile;
        use Symfony\Component\HttpFoundation\Request;
        use Symfony\Component\HttpKernel\Exception\BadRequestHttpException;
        use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
        use Symfony\Component\HttpKernel\KernelInterface;

        class MyEntityController// extends AbstractController
        {
    
        private ParameterBagInterface $params;
        private EntityManagerInterface $entityManager;
    
        public function __construct(ParameterBagInterface $params, EntityManagerInterface $entityManager)
        {
            $this->params = $params;
            $this->entityManager = $entityManager;
        }
    
        public function export(KernelInterface $kernel): array
        {
            dd("why?!");
    }
// ...
}

In ressource/entity.yaml :

resources:
  App\Entity\MyEntity:
    shortName: ~
    description: ~
    attributes:
      order:
        referenceCafHuissier: asc
        montantEcheance: asc

      # security: 'is_granted("ROLE_USER")'
      normalization_context:
        groups: ['myentity:read']
      denormalization_context:
        groups: ['myentity:write']
    properties:
    ...
    collectionOperations:
    ...
      exportcsv:
        security: 'is_granted("ROLE_MYENTITY_ADMIN")'
        # pagination_partial: true
        method: 'GET'
        path: '/myentity/export'
        controller: 'App\Controller\MyEntityController::export'
        openapi_context:
          summary: Export CSV
          parameters: []
          responses:
            '200':
              description: Génération réussi de l'export CSV
              content:
                application/json:
                  schema:
                    type: object
                    properties:
                      type:
                        type: string
                        description: mime-type du fichier.
                      content:
                        type: string
                        description: Base64 du contenu du fichier.

Solution

The query you're talking about is most likely made by the ReadListener.

To disable it, set the read property to false, as explained here :

collectionOperations:
    exportcsv:
        method: get
        path: /pensions/export
        # ...
        read: false    


Answered By - rugolinifr
Answer Checked By - Terry (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing