2

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;
    }
}
IWebb
  • 86
  • 1
  • 6
  • 1
    Have you cleared your cache after making changes (if in the PROD environment)? Command to use is: `php bin/console cache:clear --env=prod` Also if you use `http://localhost:8080/app_dev.php/` in a browser, that is the debug URL, you might get more information. Let us know the results. – Alvin Bunk Nov 18 '16 at 17:08
  • You must use `@View()` annotation – Mohammad Zare Moghadam Nov 18 '16 at 21:16
  • @AlvinBunk That was the issue thanks for your help, I will be using app_dev from now on. I still have a few errors in the code but I can deal with those now I am getting the feedback in the error log I would expect. – IWebb Nov 20 '16 at 09:44

0 Answers0