dongxu7121 2014-06-09 23:46
浏览 67
已采纳

路由在FOSRestBundle和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. With the current code, I'm just getting 404's.

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?

Thanks in advance!

  • 写回答

1条回答 默认 最新

  • douyin7829 2014-06-10 02:20
    关注

    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?

    No, you do need to generate that template file yourself and that is probably the cause for your 500 errors. 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.

    For your 404's: do a ./app/console router:debug | grep api and see if the generated routes are like what you expect.

    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

    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/

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器
  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)