Symfony 的調(diào)試工具是無與倫比的美麗. 一用就會讓開發(fā)者愛不釋手.
如果是從 symfony/website-skeleton 項(xiàng)目骨架來創(chuàng)建項(xiàng)目, debug工具包已經(jīng)包含了. 如果是從 symfony/skeleton 項(xiàng)目骨架來創(chuàng)建項(xiàng)目, 需要再引用一下 debug 包.
$ composer require debug --dev
建議使用 symfony/website-skeleton 來創(chuàng)建項(xiàng)目. 比較方便.
來看一下 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 信息的轉(zhuǎn)發(fā)地址. 默認(rèn)為: 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 信息被漂亮的發(fā)送到另外一個端口, 我們打開一個終端. 定位到項(xiàng)目根目錄. 運(yùn)行: bin/console server:dump 就可以持續(xù)追蹤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"
]
只有不關(guān)閉這個終端, 就類似 Linux 的 tail -f 非常的方便. 如果不喜歡用終端. 那么瀏覽器的 Symfony Profiler 調(diào)試工具條也提供了調(diào)試信息查看. 這個 Symfony Profiler 已經(jīng)強(qiáng)大到了令人發(fā)指的地步了.
另外, 在視圖模板 twig 文件中也可以肆意的使用 {{ dump() }} 打印任何調(diào)試信息. 調(diào)試信息會被美化的顯示, 不會破壞頁面樣式表. 非常的人性化.