FriendsOfSymfony / FOSRestBundle

This Bundle provides various tools to rapidly develop RESTful API's with Symfony
http://symfony.com/doc/master/bundles/FOSRestBundle/index.html
MIT License
2.79k stars 703 forks source link

Use annotation @Get @Post on class and not method #2308

Closed julienfollow closed 3 years ago

julienfollow commented 3 years ago

Hello Do we have a possibility to use their annotations on the class ? I use method __invoke. My pattern is ADR. One class is one action. For example =>

<?php
declare(strict_types=1);

namespace Om\Actions\User;

use FOS\RestBundle\Controller\Annotations as Rest;
use Om\Actions\Get;

/**
 * Class GetUser
 * @package Om\Actions\User
 * @Rest\Get(
 *     path = "/users/{uuid}",
 *     name = "get_user",
 *     requirements = {"uuid"="TODO"}
 * )
 */
class GetUser extends Get
{
}

image

But i have this error ... I understand error. After read doc, I found nothing about this possibility.

xabbuh commented 3 years ago

A route needs to reference a callable that can be executed by the Symfony HTTP kernel. A class is no callable in PHP. If you have an __invoke() method, you need to put the annotation on that method.

julienfollow commented 3 years ago

Thank's you @xabbuh, i do like that, the night is good to reflexion ;)

<?php
declare(strict_types=1);

namespace Om\Actions\User;

use FOS\RestBundle\Controller\Annotations as Rest;
use Om\Actions\Get;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Routing\Annotation\Route;

/**
 * Class GetUser
 * @package Om\Actions\User
 */
class GetUser extends Get
{
    /**
    * @Rest\Get(
    *     path="/users/{uuid}",
    *     name = "get_user",
    *     requirements = {"id"="\d+"}
    * )
    */
    public function __invoke(Request $request)
    {
        return parent::__invoke($request);
    }
}