I am fairly new to Symfony and I am trying to set up a Restful web service using the FOSRestBundle.
I am using the following version of Symfony and the bundle
symfony/symfony v3.1.6 The Symfony PHP framework
friendsofsymfony/rest-bundle 2.1.0 This Bundle provides various t...
I am following this article (http://jeremycurny.com/2016/03/27/symfony3-rest-api/) and seem to have everything in place however I am receiving the following error.
[2016-11-18 09:17:53] request.INFO: Matched route "{route}". {"route":"homepage","route_parameters":{"_controller":"AppBundle\\Controller\\DefaultController::indexAction","_route":"homepage"},"request_uri":"http://localhost:8080/","method":"GET"} []
[2016-11-18 09:17:53] security.INFO: Populated the TokenStorage with an anonymous Token. [] []
[2016-11-18 09:17:53] request.CRITICAL: Uncaught PHP Exception LogicException: "The controller must return a response (Object(FOS\RestBundle\View\View) given)." at /vagrant/taskiyaki/vendor/symfony/symfony/src/Symfony/Component/HttpKernel/HttpKernel.php line 171 {"exception":"[object] (LogicException(code: 0): The controller must return a response (Object(FOS\\RestBundle\\View\\View) given). at /vagrant/taskiyaki/vendor/symfony/symfony/src/Symfony/Component/HttpKernel/HttpKernel.php:171)"} []
AppKernel.php
$bundles = [
new Symfony\Bundle\FrameworkBundle\FrameworkBundle(),
new Symfony\Bundle\SecurityBundle\SecurityBundle(),
new Symfony\Bundle\TwigBundle\TwigBundle(),
new Symfony\Bundle\MonologBundle\MonologBundle(),
new Symfony\Bundle\SwiftmailerBundle\SwiftmailerBundle(),
new Doctrine\Bundle\DoctrineBundle\DoctrineBundle(),
new Sensio\Bundle\FrameworkExtraBundle\SensioFrameworkExtraBundle(),
new FOS\RestBundle\FOSRestBundle(),
new AppBundle\AppBundle(),
];
config.yml
# FOSRest Configuration
fos_rest:
body_listener: true
format_listener:
rules:
- { path: '^/', priorities: ['json'], fallback_format: json, prefer_extension: false }
param_fetcher_listener: true
view:
view_response_listener: 'force'
formats:
json: true
DefaultController.php
namespace AppBundle\Controller;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use FOS\RestBundle\Controller\Annotations as Rest;
use FOS\RestBundle\Controller\FOSRestController;
class DefaultController extends FOSRestController
{
/*
*@Rest\Get("/")
*/
public function indexAction(Request $request){
$data = ['message' => 'REST Api'];
$view = $this->view($data, Response::HTTP_OK);
return $view;
}
}