我創建了一個OneToMany關係的小型垃圾回收系統,並且還想創建一個小型API。Symfony API控制器必須返回響應
我產生了新的ApiBundle並添加1個控制器爲我的實體1,看起來像這樣:
<?php
namespace ApiBundle\Controller;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use FOS\RestBundle\Controller\Annotations as Rest;
use FOS\RestBundle\Controller\FOSRestController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use FOS\RestBundle\View\View;
use DataBundle\Entity\Job;
class JobController extends FOSRestController
{
public function getAction()
{
$result = $this->getDoctrine()->getRepository('DataBundle:Job')->findAll();
if ($result === null) {
return new View("There are no jobs in the database", Response::HTTP_NOT_FOUND);
}
return $result;
}
public function idAction($id)
{
$result = $this->getDoctrine()->getRepository('DataBundle:Job')->find($id);
if($result === null) {
return new View("Job not found", Response::HTTP_NOT_FOUND);
}
return $result;
}
}
但是,當我撥打電話到/ API /工作我得到以下錯誤:
Uncaught PHP Exception LogicException: "The controller must return a response (Array(0 => Object(DataBundle\Entity\Job), 1 => Object(DataBundle\Entity\Job)) given)."
任何人都知道我在做什麼錯在這裏?
任何幫助表示讚賞!
很多感謝:)
https://symfony.com/doc/current/controller.html –