duanque2413
2013-09-28 16:37 阅读 38
已采纳

@Route(defaults = {“_ format”=“json”}返回HTML

I have a simple crud controller generate by doctrine

<?php

namespace MyBundle\Controller;

use Symfony\Component\HttpFoundation\Request;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
use MyBundle\Entity\MyEntity;
use MyBundle\Form\MyEntityType;

/**
 * PsaEventualidadContrato controller.
 *
 * @Route("/my")
 */
class MyController extends Controller
{
    //...
    /**
     * Displays a form to create a new MyEntity entity.
     *
     * @Route("/new.{_format}", name="my_new", defaults={"_format"="json"})
     * @Method("GET")
     * @Template()
     */
    public function newAction()
    {
        $entity = new MyEntity();
        $form   = $this->createCreateForm($entity);

        return array(
            'entity' => $entity,
            'form'   => $form->createView(),
        );
    }
    //...
}

when i go to http://myhost/app_dev.php/my/new.json in dev.log i can see

[2013-09-28 11:29:13] request.INFO: Matched route "eventualidad_new" (parameters: "_format": "json", "_controller": "MyBundle\Controller\MyController::newAction", "_route": "my_new") [] []

but the browser renders the html template. why this happens?

I'm using Symfony v2.3.4 and v2.3.5

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享

1条回答 默认 最新

  • 已采纳
    dtpxi88884 dtpxi88884 2013-09-28 17:02

    The bundle FOS\RestBundle\FOSRestBundle is creating a conflict, when I remove from AppKernel.php everything works normal

    点赞 评论 复制链接分享

相关推荐