I'm trying to setup FOS Rest Bundle on symfony 4.2.3. and I'm following this tutorial:
https://www.thinktocode.com/2018/03/26/symfony-4-rest-api-part-1-fosrestbundle/
Installed and configured the bundle:
//annotations.yaml
rest_controller:
resource: ../../src/Controller/Rest/
type: annotation
prefix: /api
//fos_rest.yaml
fos_rest:
view:
view_response_listener: true
format_listener:
rules:
- { path: ^/api, prefer_extension: true, fallback_format: json, priorities: [ json ] }
and when I call postAction() of my ArticleController object, which looks like:
class ArticleController extends FOSRestController
{
/**
* Creates an Article resource
* @Rest\Post("/articles")
* @param Request $request
* @return View
*/
public function postArticle(Request $request): View
{
$entityManager = $this->getDoctrine()->getManager();
$article = new Article();
$article->setTitle($request->get('title'));
$article->setContent($request->get('content'));
$articleCategory = $entityManager->getReference(ArticleCategory::class, $request->get('article_category'));
$article->setArticleCategory($articleCategory);
$article->setPublished($request->get('published'));
$entityManager->persist($article);
$entityManager->flush();
// In case our POST was a success we need to return a 201 HTTP CREATED response
return View::create($article, Response::HTTP_CREATED);
}
}
I get the error message (testing with Postman): The controller must return a "Symfony\Component\HttpFoundation\Response" object but it returned an object of type FOS\RestBundle\View\View.
Why is that?! It's defined that postArticle() method should return a View object and I'm doing that.
One notice: my class is extending FOSRestController class, which is deprecated. I also tried extending AbstractFOSRestController, but with the same result.