0
votes

I'm trying to implement in my symfony project some api. Currently the project have many controller with standard crud, based on html table, form/validator etc.

I'm looking to the api-platform project that seam to make very easy the construction of standard rest api, and for the GET part it fit my necessities. But for the POST/PUT/DELETE part it seam a very basic persist action on an entity, and suddenly in my project, i need to do many more actions after the persist of the entity.

I've red the docs and I'm really confused on how to do that... I see two possibilities:

  • Using the event system, subscribing for the POST_WRITE for every entities

  • Creating a custom action for every create/update/delete actions of an entity

In both the case, I would have a really high number of single actions or event subscriber in the project (30/40), and it's really unconfortable to maintain. Also I probably have to replicate the same code that I already have in the controller, to maintain the old form system until is all rewitten in an API format.

Any suggestion on how to approach this problem?

There isn't a way to use the same controller actions, like in the FOSRestBundle, so that I can receive the data, do the various validation/persist/extra actions, and then return a result that is managed by the api-platform events?

Any way to manually call some part of the api-platform, like the deserialization/serialization, the filter and pagination from a standard controller action?

Thanks to all Cheers Daniele

1

1 Answers

0
votes

Forgive me if I do not completely understand your question but if you already have the functionality written in the controller and you want to access the same actions via an api then maybe you can set multiple routes on each action and depending on how the action was called you can respond differently. For example:

/**
 * @Route("/api/v1/tester", name="api_tester")
 * @Route("/tester", name="tester")
 */
public function testerAction( Request $request )
{
    $route = $request->attributes->get('_route');

    if( $route == "api_tester" )
        #..do things the api way
        response = array( "success" => 1, "data" => $return_string );
        return new Response( json_encode( $response ) );
    } else { //non-api
        $this->render('tester/basic.html.twig', array();
    }
}

You can evaluate which route was used and in various sections of your actions you can handle things differently based on if the action was called via the api or from a normal request.