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/

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

报告相同问题?

悬赏问题

  • ¥15 phython如何实现以下功能?查找同一用户名的消费金额合并—
  • ¥15 孟德尔随机化怎样画共定位分析图
  • ¥18 模拟电路问题解答有偿速度
  • ¥15 CST仿真别人的模型结果仿真结果S参数完全不对
  • ¥15 误删注册表文件致win10无法开启
  • ¥15 请问在阿里云服务器中怎么利用数据库制作网站
  • ¥60 ESP32怎么烧录自启动程序
  • ¥50 html2canvas超出滚动条不显示
  • ¥15 java业务性能问题求解(sql,业务设计相关)
  • ¥15 52810 尾椎c三个a 写蓝牙地址