我正在尝试使用最新的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
sensio_framework_extra:
view: { annotations: false }
router: { annotations: true }
fos_rest:
routing_loader:
default_format: json
format_listener: true
view:
view_response_listener: true
me帮助添加json默认格式
fos_rest:
routing_loader:
default_format: json
将此添加到您的config.yml
:
fos_rest:
format_listener:
rules:
- { path: ^/products/new, priorities: [html, json, xml], fallback_format: html, prefer_extension: false }
这应该将html扩展名设置为该路径的默认值,并且应该使它不需要扩展名。