使用FOSRestBundle进行yml路由的symfony3 REST API问题

时间:2017-06-26 12:00:17

标签: symfony routing fosrestbundle symfony-3.3

我终于在Symfony3中获得了第一个实际工作的API。 到目前为止很好,但是当我尝试使用EmailMessage email = new EmailMessage(service); email.IsDeliveryReceiptRequested = true; email.IsReadReceiptRequested = true; 配置而不是YML时出现问题。它让我疯狂,因为它似乎工作,事实上,当我有机会,例如控制器名称,它给我一个内部服务器错误,但是,当一切都“正确”,它似乎无法在我的控制器中找到该方法。 所以这是我的代码:

常规路由设置

annotation

捆绑路由设置

#config/routing.yml
user_routes:
    resource: "@AppBundle/Resources/config/user_routes.yml"
    type: rest

最后我的控制器:

#AppBundle/Resources/config/user_routes.yml
user:
    type: rest
    resource: AppBundle\Controller\UserController

使用 <?php namespace AppBundle\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 AppBundle\Entity\User; class UserController extends FOSRestController { // /** // * @Rest\Get("/user") // */ public function getAction() { $restresult = $this->getDoctrine()->getRepository('AppBundle:User')->findAll(); if ($restresult === null) { return new View("there are no users exist", Response::HTTP_NOT_FOUND); } return $restresult; } } 它运行良好,每个人都很高兴但是当我使用这些设置时,我得到了404.我尝试根据Symfony官方文档添加annotationprefix: /api,但它没有工作。 我还尝试添加name_prefix: api_但错误404总是在拐角处。 正如我所说的,如果我在defaults: { _controller: AppBundle:Controller:UserController:get中更改控制器类的名称,我会收到500错误,因此看起来正在读取路由,但很明显这里缺少某些东西,我无法找到它无论是官方文件还是其他地方。

2 个答案:

答案 0 :(得分:0)

从此处更改操作的名称:

public function getAction()

到此:

public function getUsersAction()

DOCUMENTATION

答案 1 :(得分:0)

好吧,为了防止它对某人有用,我找到了解决方案。 问题在于yml user_routes.yml。 这是正确的配置:

path: /user
defaults: { _controller: AppBundle:User:get }
requirements:
    _method: GET

那是它;)