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

Router not working as described #1094

Closed NullDivision closed 4 years ago

NullDivision commented 9 years ago

So, I'm trying to integrate FOSRest into my project. I've tried adding the same thing as in the description on the Symphony website but apparently it doesn't work.

This is my router: app/config/routing.yml

template_card:
    type:     rest
    resource: "@TemplateCardBundle/Controller/"

NelmioApiDocBundle:
    resource: "@NelmioApiDocBundle/Resources/config/routing.yml"
    prefix:   /api/doc

...and the controller: src/Template/CardBundle/Controller/CardController.php

<?php

namespace Template\CardBundle\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;

/**
 * Card API functions
 */
class CardsController extends Controller
{
    public function getAction()
    {
    } // "get_cards"    [GET] /cards
}

I get the error:

[Sun Aug 16 17:20:44 2015] PHP Fatal error:  Uncaught exception 'InvalidArgumentException' with message 'Can't find class for controller "@TemplateCardBundle/Controller"' in /home/NullDivision/Projects/tpl00001/server/vendor/friendsofsymfony/rest-bundle/FOS/RestBundle/Routing/Loader/RestRouteLoader.php:111
Stack trace:
#0 /home/NullDivision/Projects/tpl00001/server/vendor/friendsofsymfony/rest-bundle/FOS/RestBundle/Routing/Loader/RestRouteLoader.php(72): FOS\RestBundle\Routing\Loader\RestRouteLoader->getControllerLocator('@TemplateCardBu...')
#1 /home/NullDivision/Projects/tpl00001/server/vendor/symfony/config/Loader/FileLoader.php(112): FOS\RestBundle\Routing\Loader\RestRouteLoader->load('@TemplateCardBu...', 'rest')
#2 /home/NullDivision/Projects/tpl00001/server/vendor/symfony/routing/Loader/YamlFileLoader.php(175): Symfony\Component\Config\Loader\FileLoader->import('@TemplateCardBu...', 'rest', false, '/home/NullDivision/...')
#3 /home/NullDivision/Projects/tpl00001/server/vendor/symfony/routing/Loader/YamlFileLoader.php(93): Symfony\Component\Routing\Loader\YamlFileLoader- in /home/NullDivision/Projects/tpl00001/server/vendor/symfony/config/Loader/FileLoader.php on line 130
[Sun Aug 16 17:20:44 2015] 127.0.0.1:56023 [500]: /app_dev.php/_profiler/477649?panel=router - Uncaught exception 'InvalidArgumentException' with message 'Can't find class for controller "@TemplateCardBundle/Controller"' in /home/NullDivision/Projects/tpl00001/server/vendor/friendsofsymfony/rest-bundle/FOS/RestBundle/Routing/Loader/RestRouteLoader.php:111
Stack trace:
#0 /home/NullDivision/Projects/tpl00001/server/vendor/friendsofsymfony/rest-bundle/FOS/RestBundle/Routing/Loader/RestRouteLoader.php(72): FOS\RestBundle\Routing\Loader\RestRouteLoader->getControllerLocator('@TemplateCardBu...')
#1 /home/NullDivision/Projects/tpl00001/server/vendor/symfony/config/Loader/FileLoader.php(112): FOS\RestBundle\Routing\Loader\RestRouteLoader->load('@TemplateCardBu...', 'rest')
#2 /home/NullDivision/Projects/tpl00001/server/vendor/symfony/routing/Loader/YamlFileLoader.php(175): Symfony\Component\Config\Loader\FileLoader->import('@TemplateCardBu...', 'rest', false, '/home/NullDivision/...')
#3 /home/NullDivision/Projects/tpl00001/server/vendor/symfony/routing/Loader/YamlFileLoader.php(93): Symfony\Component\Routing\Loader\YamlFileLoader- in /home/NullDivision/Projects/tpl00001/server/vendor/symfony/config/Loader/FileLoader.php on line 130
NullDivision commented 9 years ago

I've also tried to use @TemplateCardBundle/Controller/CardController but it trips Symphony's kernel saying:

[Sun Aug 16 17:20:27 2015] PHP Fatal error:  Uncaught exception 'InvalidArgumentException' with message 'Unable to find file "@TemplateCardBundle/Controller/CardController".' in /home/ancarius/Projects/tpl00001/server/vendor/symfony/http-kernel/Kernel.php:313
Stack trace:
#0 /home/ancarius/Projects/tpl00001/server/vendor/symfony/http-kernel/Config/FileLocator.php(51): Symfony\Component\HttpKernel\Kernel->locateResource('@TemplateCardBu...', '/home/ancarius/...', true)
#1 /home/ancarius/Projects/tpl00001/server/vendor/friendsofsymfony/rest-bundle/FOS/RestBundle/Routing/Loader/RestRouteLoader.php(107): Symfony\Component\HttpKernel\Config\FileLocator->locate('@TemplateCardBu...')
#2 /home/ancarius/Projects/tpl00001/server/vendor/friendsofsymfony/rest-bundle/FOS/RestBundle/Routing/Loader/RestRouteLoader.php(72): FOS\RestBundle\Routing\Loader\RestRouteLoader->getControllerLocator('@TemplateCardBu...')
#3 /home/ancarius/Projects/tpl00001/server/vendor/symfony/config/Loader/FileLoader.php(112): FOS\RestBundle\Routing\Loader\RestRouteLoader->load('@TemplateCard in /home/ancarius/Projects/tpl00001/server/vendor/symfony/config/Loader/FileLoader.php on line 130
[Sun Aug 16 17:20:27 2015] 127.0.0.1:56021 [500]: /app_dev.php/_profiler/477649?panel=router - Uncaught exception 'InvalidArgumentException' with message 'Unable to find file "@TemplateCardBundle/Controller/CardController".' in /home/ancarius/Projects/tpl00001/server/vendor/symfony/http-kernel/Kernel.php:313
Stack trace:
#0 /home/ancarius/Projects/tpl00001/server/vendor/symfony/http-kernel/Config/FileLocator.php(51): Symfony\Component\HttpKernel\Kernel->locateResource('@TemplateCardBu...', '/home/ancarius/...', true)
#1 /home/ancarius/Projects/tpl00001/server/vendor/friendsofsymfony/rest-bundle/FOS/RestBundle/Routing/Loader/RestRouteLoader.php(107): Symfony\Component\HttpKernel\Config\FileLocator->locate('@TemplateCardBu...')
#2 /home/ancarius/Projects/tpl00001/server/vendor/friendsofsymfony/rest-bundle/FOS/RestBundle/Routing/Loader/RestRouteLoader.php(72): FOS\RestBundle\Routing\Loader\RestRouteLoader->getControllerLocator('@TemplateCardBu...')
#3 /home/ancarius/Projects/tpl00001/server/vendor/symfony/config/Loader/FileLoader.php(112): FOS\RestBundle\Routing\Loader\RestRouteLoader->load('@TemplateCard in /home/ancarius/Projects/tpl00001/server/vendor/symfony/config/Loader/FileLoader.php on line 130
Zeichen32 commented 9 years ago

You need to use the full class name:

template_card:
    type:     rest
    resource: Template\CardBundle\Controller\CardsController

See: http://symfony.com/doc/master/bundles/FOSRestBundle/5-automatic-route-generation_single-restful-controller.html#single-restful-controller-routes

NullDivision commented 9 years ago

Tried that too. See my second message.

Zeichen32 commented 9 years ago

In your second message, you use @TemplateCardBundle not the full class name!

NullDivision commented 9 years ago

I've tried using the full qualified namespace. The result is the same. Unless I add .php to the end (which basically just loads the PHP file as a resource it doesn't work.

lsmith77 commented 9 years ago

can you try this syntax?

https://github.com/gimler/symfony-rest-edition/blob/2.3/app/config/routing_dev.yml#L9