【问题标题】:Symfony FosRestBundle - form route without formatSymfony FosRestBundle - 没有格式的形成路线
【发布时间】:2014-05-29 08:13:41
【问题描述】:

我正在尝试使用最新的 Symfony (2.4.5) 和 FOSRestBundle (dev-master 6b384c1) 引导简单的 REST 应用程序。

我的配置:

fos_rest:
    format_listener: true
    view:
        view_response_listener: true

sensio_framework_extra:
    view:    { annotations: false }
    router:  { annotations: true }

我的路线:

products:
    type:     rest
    resource: Telemetrik\Bundle\ProductBundle\Controller\ProductsController

控制器:

<?php

// namespace & imports

class ProductsController extends Controller
{
    /**
     * @return Form
     * @View("TelemetrikProductBundle::form.html.twig")
     */
    public function newProductAction()
    {
        return $this->getForm();
    }

    /**
     * @param Request $request
     * @return View|Form
     * @View
     */
    public function postProductsAction(Request $request)
    {
        $form = $this->getForm();

        $form->handleRequest($request);

        if ($form->isValid()) {
            // Logic placeholder
        }

        return $form;
    }

    protected function getForm()
    {
        return $this->createForm(new ProductType());
    }
}

当使用router:debug 我得到:

new_product              GET    ANY    ANY  /products/new.{_format}
post_products            POST   ANY    ANY  /products.{_format}

这很好,但是因为newProductAction 应该是一种形式:

  • 我不希望它可以从 HTML 以外的格式访问
  • 我想从/products/new 而不是/products/new.html 访问我的表单(这似乎是我可以访问该资源的唯一选项)。如果我去products/new 我得到:Format '' not supported, handler must be implemented

【问题讨论】:

    标签: php symfony fosrestbundle


    【解决方案1】:
    sensio_framework_extra:
        view:    { annotations: false }
        router:  { annotations: true }
    
    fos_rest:
        routing_loader:
            default_format: json
        format_listener: true
        view:
            view_response_listener: true
    

    【讨论】:

      【解决方案2】:

      我帮助将格式默认添加到 json

      fos_rest:
          routing_loader:
              default_format: json
      

      【讨论】:

        【解决方案3】:

        将此添加到您的config.yml

        fos_rest:
            format_listener:
                rules:
                    - { path: ^/products/new, priorities: [html, json, xml], fallback_format: html, prefer_extension: false }
        

        这应该将 html 扩展名设置为此路径的默认值,并且应该使其不需要扩展名。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2016-06-04
          • 2018-10-17
          • 1970-01-01
          • 1970-01-01
          • 2018-12-22
          • 1970-01-01
          • 2016-01-07
          • 1970-01-01
          相关资源
          最近更新 更多