Symfony2 + FOSRestBundle + FrameworkExtraBundle 使用@Template 时禁用休息和错误
Symfony2 + FOSRestBundle + FrameworkExtraBundle Disable rest and error when using @Template
我正在尝试设置我的应用程序,以便同一操作可以 return 响应 Web 和 api。这很好用,但有时只需要对网站执行操作,而不是 api。我正在根据 action/controller.
寻找一种 enable/disable 此功能的方法
我已经创建了两个演示操作来进行测试,但无法让其中一个 not 响应 api url。
可通过 domain.com
访问网站
Api 可通过 api 访问。domain.com - 所有对 api 的请求都被格式化为 json 在配置中使用格式侦听器
- { path: '^/', host: 'api.domain.dev', priorities: ['json'], fallback_format: ~, prefer_extension: false }
第一个操作 (indexAction) return 对 HTML 和 Json 都有效,具体取决于您使用的域。问题出在第二个动作(testAction)上,我希望它只适用于网站域。访问 api.domain.com/company-test 时,响应是模板 HTML,content-type header 设置为 application/json。
当 JSON 版本不可用且正在使用 @Template 时,是否有办法使此 return 成为错误(404?)?
测试操作
use FOS\RestBundle\Controller\Annotations as Rest;
use FOS\RestBundle\View\View;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
use Nelmio\ApiDocBundle\Annotation\ApiDoc;
/**
* Get a list of companies
*
* @Rest\Get("/company")
* @Rest\View(
* template = "CompanyBundle:Company:index.html.twig",
* templateVar = "companies",
* statusCode = 200,
* )
*
* @ApiDoc(
* description=""
* )
*/
public function indexAction() {
$em = $this->getDoctrine()->getManager();
$companies = $em->getRepository('CompanyBundle:Company')->findAll();
$view = View::create();
$view->setData($companies);
$view->setTemplateData(['extraData' => 'Hello World!']);
return $view;
}
/**
* Get a list of companies
*
* @Route("/company-test")
*
* @ApiDoc(
* description=""
* )
*
* @Template(
* template="CompanyBundle:Company:index.html.twig"
* )
*/
public function testAction() {
$em = $this->getDoctrine()->getManager();
$companies = $em->getRepository('CompanyBundle:Company')->findAll();
return [
'companies' => $companies,
'test-test' => 'Hello World!'
];
}
我认为你可以从 SensioFrameworkExtraBundle 扩展 TemplateListener 并修改 "onKernelView" https://github.com/sensiolabs/SensioFrameworkExtraBundle/blob/master/EventListener/TemplateListener.php#L77
创建你自己的侦听器 MyBundle\EvetListener\MyListener.php 扩展原始 TemplateListener,然后你可以整个 onKernelView 方法并将 https://github.com/sensiolabs/SensioFrameworkExtraBundle/blob/master/EventListener/TemplateListener.php#L83 更改为
if (null === $template) {
return;
} elseif($this->getRequest()->getHost() == $this->container->getParameter('api_domain')) { // %api_domain% should be filled at parameters.yml
$event->setResponse(new JsonResponse(NULL, Response::HTTP_NOT_FOUND)); // don't forget to add "use Symfony\Component\HttpFoundation\JsonResponse"
return;
}
覆盖服务参数https://github.com/sensiolabs/SensioFrameworkExtraBundle/blob/master/Resources/config/view.xml#L8
作为
<parameters>
<parameter key="sensio_framework_extra.view.listener.class">MyApp\EventListener\MyListener</parameter>
</parameters>
或通过 yml 如果您使用 yml 而不是 xml 进行服务配置
我正在尝试设置我的应用程序,以便同一操作可以 return 响应 Web 和 api。这很好用,但有时只需要对网站执行操作,而不是 api。我正在根据 action/controller.
寻找一种 enable/disable 此功能的方法我已经创建了两个演示操作来进行测试,但无法让其中一个 not 响应 api url。
可通过 domain.com
访问网站Api 可通过 api 访问。domain.com - 所有对 api 的请求都被格式化为 json 在配置中使用格式侦听器
- { path: '^/', host: 'api.domain.dev', priorities: ['json'], fallback_format: ~, prefer_extension: false }
第一个操作 (indexAction) return 对 HTML 和 Json 都有效,具体取决于您使用的域。问题出在第二个动作(testAction)上,我希望它只适用于网站域。访问 api.domain.com/company-test 时,响应是模板 HTML,content-type header 设置为 application/json。
当 JSON 版本不可用且正在使用 @Template 时,是否有办法使此 return 成为错误(404?)?
测试操作
use FOS\RestBundle\Controller\Annotations as Rest;
use FOS\RestBundle\View\View;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
use Nelmio\ApiDocBundle\Annotation\ApiDoc;
/**
* Get a list of companies
*
* @Rest\Get("/company")
* @Rest\View(
* template = "CompanyBundle:Company:index.html.twig",
* templateVar = "companies",
* statusCode = 200,
* )
*
* @ApiDoc(
* description=""
* )
*/
public function indexAction() {
$em = $this->getDoctrine()->getManager();
$companies = $em->getRepository('CompanyBundle:Company')->findAll();
$view = View::create();
$view->setData($companies);
$view->setTemplateData(['extraData' => 'Hello World!']);
return $view;
}
/**
* Get a list of companies
*
* @Route("/company-test")
*
* @ApiDoc(
* description=""
* )
*
* @Template(
* template="CompanyBundle:Company:index.html.twig"
* )
*/
public function testAction() {
$em = $this->getDoctrine()->getManager();
$companies = $em->getRepository('CompanyBundle:Company')->findAll();
return [
'companies' => $companies,
'test-test' => 'Hello World!'
];
}
我认为你可以从 SensioFrameworkExtraBundle 扩展 TemplateListener 并修改 "onKernelView" https://github.com/sensiolabs/SensioFrameworkExtraBundle/blob/master/EventListener/TemplateListener.php#L77
创建你自己的侦听器 MyBundle\EvetListener\MyListener.php 扩展原始 TemplateListener,然后你可以整个 onKernelView 方法并将 https://github.com/sensiolabs/SensioFrameworkExtraBundle/blob/master/EventListener/TemplateListener.php#L83 更改为
if (null === $template) {
return;
} elseif($this->getRequest()->getHost() == $this->container->getParameter('api_domain')) { // %api_domain% should be filled at parameters.yml
$event->setResponse(new JsonResponse(NULL, Response::HTTP_NOT_FOUND)); // don't forget to add "use Symfony\Component\HttpFoundation\JsonResponse"
return;
}
覆盖服务参数https://github.com/sensiolabs/SensioFrameworkExtraBundle/blob/master/Resources/config/view.xml#L8
作为
<parameters>
<parameter key="sensio_framework_extra.view.listener.class">MyApp\EventListener\MyListener</parameter>
</parameters>
或通过 yml 如果您使用 yml 而不是 xml 进行服务配置