简体   繁体   English

在FOSRestBundle和Symfony中不起作用的路线

[英]Routes not working in FOSRestBundle and Symfony

I've been going round in circles with this one. 我一直在和这个人转转。 I've been either getting 500 errors saying template cannot be rendered or found, or when I attempt to use the annotations, they clash with the fact I'm using Symfony's annotations for my routes. 我要么收到500个错误,说无法渲染或找到模板,要么当我尝试使用注释时,它们与我在路线上使用Symfony注释的事实相冲突。 With the current code, I'm just getting 404's. 使用当前代码,我刚得到404。

My config: 我的配置:

# config.yml
fos_rest:
routing_loader:
    default_format: json

An example of my controller: 我的控制器的一个例子:

namespace IGIG\GigBundle\Controller;

use FOS\RestBundle\Controller\FOSRestController;
use FOS\RestBundle\Controller\Annotations\Route;

use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;

use IGIG\GigBundle\Document\Gig;

class GigApiController extends FOSRestController
{
    public function getGigsAction()
    {
        $gigs = $this->get('doctrine_mongodb')
                     ->getRepository('IGIGGigBundle:Gig')
                     ->findAll();

        $view = $this->view($gigs, 200)
            ->setTemplate("IGIG:GigBundle:getGigs.html.twig")
            ->setTemplateVar('gigs');

        return $this->handleView($view);
    }
 }

Routing 路由

gigs:    
  prefix: /api   
  type: rest    
  resource: IGIG\GigBundle\Controller\GigApiController

I should also add, the .html.twig file within the controller doesn't actually exist, I was under the impression that it was automatically generated, is that the case? 我还应该补充一点,控制器中的.html.twig文件实际上不存在,给我的印象是它是自动生成的,是这样吗?

Thanks in advance! 提前致谢!

First of all it looks like your config is not indented properly, but might also just be a copy/paste error, otherwise it should throw an exception for you. 首先,看起来您的配置没有正确缩进,但也可能只是复制/粘贴错误,否则它将为您抛出异常。

# config.yml
fos_rest:
    routing_loader:
        default_format: json

I should also add, the .html.twig file within the controller doesn't actually exist, I was under the impression that it was automatically generated, is that the case? 我还应该补充一点,控制器中的.html.twig文件实际上不存在,给我的印象是它是自动生成的,是这样吗?

No, you do need to generate that template file yourself and that is probably the cause for your 500 errors. 不,您确实需要自己生成该模板文件,这可能是导致500错误的原因。 However if you don't intend to use twig templates next to your REST API, so if you ever only want to return JSON responses, then you won't need them at all and can remove the calls from your controller too. 但是,如果您不想在REST API旁边使用树枝模板,那么如果您只想返回JSON响应,则根本不需要它们,并且也可以从控制器中删除调用。

For your 404's: do a ./app/console router:debug | grep api 对于您的404:执行./app/console router:debug | grep api ./app/console router:debug | grep api and see if the generated routes are like what you expect. ./app/console router:debug | grep api ,查看生成的路由是否符合您的期望。

You should have a look at the bundle documentation again, in particular the full config reference: https://github.com/FriendsOfSymfony/FOSRestBundle/blob/master/Resources/doc/configuration-reference.md 您应该再次查看捆绑软件文档,尤其是完整的配置参考: https : //github.com/FriendsOfSymfony/FOSRestBundle/blob/master/Resources/doc/configuration-reference.md

And I found this very helpful too when starting out with REST APIs with Symfony: http://welcometothebundle.com/symfony2-rest-api-the-best-2013-way/ 而且,当我开始使用带有Symfony的REST API时,这也非常有帮助: http : //welcometothebundle.com/symfony2-rest-api-the-best-2013-way/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM