Search code examples
phpsymfonylifecycle

Controller check requirements to process action


I'm trying to figure out if there is possibility to check requirements before processing controller action. In Nette there are methods like checkRequirements, onStartup, beforeRender where I can check this.

I have api resource album/{albumId}/song/ and I would like to check if album with given id exists every time any action on my SongController is processed and return 404 status code if not.

So far I have found this article in Symfony documentation where I found there are no methods like preExecute and postExecute. However I guess there is bundle or something like that to add those methods. I think it does not make sense to create new class to use it only in one controller.

Are there any other options to do it?


Solution

  • ParamConverter does that. It looks for a entity using the id supplied from the route and throws an exception, returning a 404 if it doesn't find anything.

    // paramConverter requires that you type-hint a class, which is a best practice anyway :)
    public function getArtist(\Appbundle\Entity\Song $song)
    {
        //...
    }