我正在使用Symfony2 + FOSRest捆绑包开发REST api。
我想知道是否有任何方式可以app_dev.php
从浏览器(对应于Accept: text/html,application/xhtml+xml
标头)以开发模式()调用api来以“指定格式”显示响应,并用symfony提供的分析器将其包装在html中。
它将允许直接在浏览器中调试对api的调用。
编辑:我不想调试HTTP请求,而是整个过程(路由匹配,涉及的数据库查询等)。这就是为什么我要访问symfony探查器。
从Symfony 2.4开始,分析器在HTTP标头中设置了两个附加设置:X-Debug-Token
和X-Debug-Token-Link
。(请参阅http://symfony.com/blog/new-in-symfony-2-4-quicker-access-to-the-profiler-when-working-on-an-api)
这些标头包含令牌和指向当前请求的探查器的直接链接。如果启用了探查器,则始终发送它们。
毫不奇怪,Chrome已经有一个扩展程序,可以检查这些标头的存在并提供额外的信息:Symfony2 Profiler快捷方式
在我看来,这比任何自定义的html-wrapper更好,但这仅适用于GET和POST请求-PUT和DELETE请求比较棘手。在这里,您可以使用http客户端(例如chrome-extension POSTMAN),并通过打开http标头中提供的链接来手动打开探查器,X-Debug-Token-Link
或者保持探查器页面(fe http://example.org/_profiler/)打开。
开发JSON或XML API时未显示WebDebugToolbar的原因是该工具栏设置为仅注入HTML类型的响应中。
为了解决这个问题,您可以kernel.response
在Bundle中添加一个事件侦听器,该事件侦听器会将JSON或XML响应转换为HTML。
namespace Acme\APIBundle\Event\Listener;
use Symfony\Component\HttpKernel\Event\FilterResponseEvent;
class ConvertToHtmlResponse {
public function onKernelResponse(FilterResponseEvent $event) {
if (!$event->isMasterRequest()) {
return;
}
$request = $event->getRequest();
// Only send back HTML if the requestor allows it
if (!$request->headers->has('Accept') || (false === strpos($request->headers->get('Accept'), 'text/html'))) {
return;
}
$response = $event->getResponse();
switch ($request->getRequestFormat()) {
case 'json':
$prettyprint_lang = 'js';
$content = json_encode(json_decode($response->getContent()), JSON_PRETTY_PRINT);
break;
case 'xml':
$prettyprint_lang = 'xml';
$content = $response->getContent();
break;
default:
return;
}
$response->setContent(
'<html><body>' .
'<pre class="prettyprint lang-' . $prettyprint_lang . '">' .
htmlspecialchars($content) .
'</pre>' .
'<script src="https://cdnjs.cloudflare.com/ajax/libs/prettify/r298/run_prettify.min.js"></script>' .
'</body></html>'
);
// Set the request type to HTML
$response->headers->set('Content-Type', 'text/html; charset=UTF-8');
$request->setRequestFormat('html');
// Overwrite the original response
$event->setResponse($response);
}
}
然后,您只需要在kernel.response
事件包中注册侦听器,我建议您仅在开发环境配置中这样做。
services:
# ...
acme.listener.kernel.convert_html:
class: Acme\APIBundle\Event\Listener\ConvertToHtmlResponse
tags:
- { name: kernel.event_listener, event: kernel.response }
您可以打开一个单独的浏览器,然后浏览到... / app_dev.php / _profiler /,在那里您将找到对app_dev.php的所有请求,包括外部匹配,涉及的数据库查询等。
使用FOSRestBundle,我使用特殊的模板在html页面中显示数据,因此使用调试工具栏。
在带有注释的控制器中(您也可以使用相应的方法):
@View(template="AppBundle:Api:data.html.twig", templateVar="data")
在模板中,选择您喜欢的任何格式:
<body>
<pre>{{ data | serialize('json') }}</pre>
</body>
显然,这是一种快速而肮脏的解决方案,但是可以完成工作。它还限制了在这些路由上显示实际html页面的能力。
我使用WithProfilerTrait
仅当您浏览到浏览器中的URL时才应用工具栏的my ,如果是ajax请求,则跳过更改。
// in Controller:
return $this->withProfiler($response, $request->isXmlHttpRequest());
trait WithProfilerTrait
{
protected function withProfiler(Response $response, bool $skip = false): Response
{
if ($skip === true) {
return $response;
}
$wrappedContent = '<body><pre>' . $response->getContent() . '</pre></body>';
$response->headers->set('Content-Type', 'html');
return $response->setContent($wrappedContent);
}
}
文章标签:fosrestbundle , php , symfony
版权声明:本文为原创文章,版权归 admin 所有,欢迎分享本文,转载请保留出处!
评论已关闭!