问题描述
我正在使用 Symfony2 + FOSRest 包开发 REST api.
I'm developing a REST api with Symfony2 + FOSRest bundle.
我想知道是否有任何方法可以从浏览器(对应于一个Accept: text/html,application/xhtml+xml
header) 以指定格式"显示响应,用 symfony 提供的分析器包装在 html 中.
I would like to know if there is any way for a call to the api in dev mode (app_dev.php
) from the browser (corresponding to a Accept: text/html,application/xhtml+xml
header) to display the response in the "specified format", wrapped in html with the profiler provided by symfony.
它允许直接在浏览器中调试对 api 的调用.
It would allow to debug calls to the api directly in the browser.
我不想调试 HTTP 请求,而是调试整个过程(路由匹配、涉及的数据库查询等).这就是我想要访问 symfony 分析器的原因.
I don't want to debug the HTTP request but the whole process (route matching, DB queries involved, etc). That's why I want to have access to the symfony profiler.
推荐答案
自 Symfony 2.4 起,分析器在 HTTP 标头中设置了两个附加设置:X-Debug-Token
和 X-调试令牌链接
.(参见 http://symfony.com/blog/new-in-symfony-2-4-quicker-access-to-the-profiler-when-working-on-an-api)
Since Symfony 2.4, the profiler sets two additional settings in the HTTP header: X-Debug-Token
and X-Debug-Token-Link
. (see http://symfony.com/blog/new-in-symfony-2-4-quicker-access-to-the-profiler-when-working-on-an-api)
这些标头包含令牌和指向当前请求的分析器的直接链接.如果启用了探查器,它们总是会被发送.
These headers contain the token and the direct link to the profiler for the current request. They are always sent if the profiler is enabled.
毫不奇怪,Chrome 已经有一个扩展程序可用,它会检查这些标头是否存在并提供额外信息:Symfony2 Profiler 快捷方式
Not surprisingly, there is already an extension available for Chrome, that checks for the existence of these headers and provide extra information: Symfony2 Profiler shortcut
在我看来,这比任何自定义 html-wrapper 都要好,但这仅适用于 GET 和 POST 请求 - PUT 和 DELETE 请求有点棘手.在那里您可以使用 http 客户端,例如 chrome-extension POSTMAN 并打开分析器通过打开 http-header X-Debug-Token-Link
中提供的链接手动或保留您的分析器页面(fe http://example.org/_profiler/) 已打开.
In my opinion, this is better than any custom html-wrapper, but this only works for GET and maybe POST requests - PUT and DELETE requests are a bit trickier. There you can use a http client, like the chrome-extension POSTMAN and open the profiler manually by opening the link provided in the http-header X-Debug-Token-Link
or keep your profiler-page (f.e. http://example.org/_profiler/) opened.
这篇关于如何为浏览器中的 API 请求显示 symfony 分析器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!