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 702 forks source link

Class could not be determined for Controller identified by \"AppBundle\\Controller\\Api\\find\" #1863

Closed pdugas closed 4 years ago

pdugas commented 6 years ago

I have a private utility method in an API controller that I wanted to use in another controller so I created a trait, moved the function there, and use'd the trait in both controllers. The method has a line like so:

$obj = $this->getDoctrine()->getRepository(Entity::class)->find($id);

After moving this function to the trait, I'm now getting the error below.

Class could not be determined for Controller identified by \"AppBundle\\Controller\\Api\\find\" in \/...\/src\/AppBundle\/Resources\/config\/routing_api.yml (which is being imported from \"\/...\/app\/config\/routing.yml\"). Make sure there is a loader supporting the \"rest\" type.

It seems the call to find($id) isn't being recognized properly. Curiously, if I simply move the method back to the controller class, everything is fine. I'm at a loss here. Feels to me like I'm missing something silly but I've been looking at it for a couple days now and can't find the problem. Help!

pdugas commented 6 years ago

Digging deeper, it looks like the issue may be that I put the trait in the same namespace/folder I designated as REST controllers.

api:
    type: rest
    name_prefix:  api_
    resource: '@AppBundle/Controller/Api'

The trait is AppBundle\Controller\Api\ApiControllerTrait. I'm going to move the trait out of the Api namespace and see if that fixes it. If it does, I wonder if FOSRest should be smart enough to ignore members of that namespace that aren't classes.

pdugas commented 6 years ago

Moving the trait out of the namespace fixed my problem. I'm not sure of the merits or implications of adding logic to ignore non-class members of the resource namespace. I have no problem with this being closed if someone with stronger FOSRest foo decides to.

UweM commented 6 years ago

I'm having the same problem right now. It has to do with ::class: FOSRestBundle tries to find the first class in a file by tokenizing the file, looking for T_CLASS and using the next T_STRING as class name: https://github.com/FriendsOfSymfony/FOSRestBundle/blob/7219cfba01ec8845bbfd4ace484fc61e1f8e3eef/Routing/Loader/ClassUtils.php#L30-L52 Well, Entity::class is also T_CLASS and the next T_STRING is the method name, in this case "find". I use a string with the fully qualified class name as a workaround.

MeihdiDjebli commented 5 years ago

Hi everyone,

Thanks to @UweM it works for me.

But is this a bug ? ClassUtils should be able to handle Trait mecanism ? Maybe, can we add something like that ?

// (...)
if (T_TRAIT === $token[0])
{
    return false;
}
Tobion commented 4 years ago

Closing as automatic route generation is deprecated: #1954