Symfony 的调试工具是无与伦比的美丽. 一用就会让开发者爱不释手.
如果是从 symfony/website-skeleton
项目骨架来创建项目, debug
工具包已经包含了. 如果是从 symfony/skeleton
项目骨架来创建项目, 需要再引用一下 debug
包.
$ composer require debug --dev
建议使用 symfony/website-skeleton
来创建项目. 比较方便.
来看一下 config/packages/dev/debug.yaml
的位置文件:
debug:
# Forwards VarDumper Data clones to a centralized server allowing to inspect dumps on CLI or in your browser.
# See the "server:dump" command to start a new server.
dump_destination: "tcp://%env(VAR_DUMPER_SERVER)%"
非常的简单. 只是配置了 Debug 信息的转发地址. 默认为: tcp://127.0.0.1:9912
简单使用起来, 通常我们 debug 信息一部分是 php 代码中. 比如控制器中. 比如这样: 我们 debug 出 Request 的 Header 信息.
class DefaultController extends AbstractController
{
/**
* @Route("/", name="home")
*/
public function index(Request $request)
{
$header = $request->server->getHeaders();
dump($header);
return $this->render('default/index.html.twig', [
'locale' => 'zh',
]);
}
}
这个 debug 信息被漂亮的发送到另外一个端口, 我们打开一个终端. 定位到项目根目录. 运行: bin/console server:dump
就可以持续追踪PHP代码中的 dump 信息.
GET http://localhost:8000/
--------------------------
------------ -------------------------------------------
date Tue, 12 Mar 2019 08:33:52 +0000
controller "App\Controller\DefaultController::index"
source DefaultController.php on line 17
file src/Controller/DefaultController.php
------------ -------------------------------------------
Open source in your IDE/browser:
http://localhost:8000/_profiler/open?file=src/Controller/DefaultController.php&line=17#line17
array:9 [
"HOST" => "localhost:8000"
"CONNECTION" => "keep-alive"
"CACHE_CONTROL" => "max-age=0"
"UPGRADE_INSECURE_REQUESTS" => "1"
"USER_AGENT" => "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3626.121 Safari/537.36"
"ACCEPT" => "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8"
"REFERER" => "http://localhost:8000/_profiler/4ea74f?panel=request"
"ACCEPT_ENCODING" => "gzip, deflate, br"
"ACCEPT_LANGUAGE" => "zh-CN,zh;q=0.9"
]
只有不关闭这个终端, 就类似 Linux 的 tail -f
非常的方便. 如果不喜欢用终端. 那么浏览器的 Symfony Profiler
调试工具条也提供了调试信息查看. 这个 Symfony Profiler
已经强大到了令人发指的地步了.
另外, 在视图模板 twig 文件中也可以肆意的使用 {{ dump() }} 打印任何调试信息. 调试信息会被美化的显示, 不会破坏页面样式表. 非常的人性化.