使用VSCode調試單個PHP文件

忽然發現是可使用 VSCode 調試單個 PHP 文件的,今天以前一直沒有弄成功,還覺得 VSCode 是不能調試單文件呢。這裏記錄一下今天這個「忽然發現」的過程。php

開始,是在看 Modern PHP 這本書,看到 "Built-in HTTP Server" 一節,本身測試了啓動PHP內置服務器軟件的命令:php -S localhost:4000,成功看到瀏覽器頁面顯示出相關頁面。與 Apache 設置的 Web 網站的效果是同樣的。json

而後我忽然就想到,能不能調試在內置服務器中運行的PHP代碼呢?此時,我並無意識到相關的東西。只是在網絡上搜索 xdebug php build-in server 等關鍵詞,找到了別人已經在 stackoverflow 上提過的問題,這很正常,一般你能提出的問題,別人可能早已經提過了。這個問題下面有我的給出了一個連接:vim-xdebug-and-php-54s-development-server ,點進去看了一下,部分原文引用以下:vim

I recently began using xdebug and vim for debugging and breakpoints. I had been using Eclipse and xdebug, and missed this feature when I switched back to vim.瀏覽器

I found a few articles on the subject, but most were fairly old, missed some key points and didn't mention the 5.4 development server. I used this article as my starting point, but I had to fill in a few gaps.服務器

  1. First off, make sure xdebug is installed by checking phpinfo for the section on xdebug. I use the ppa/ondrej repository for managing PHP installations and was able to install xdebug through apt. Installation was as simple as apt-get install php5-xdebug.
  2. Once you have xdebug installed, you'll need to install the vim remote debugger interface. It is a straightforward install - just copy the files into your plugin directory. For me, this was ~/.vim/plugin.
  3. Now, you have to modify your php.ini file in order to allow remote debugging. I also turned on remote autostart so I don't have to deal with the ?XDEBUG_SESSION_START query string on my development server. Since I was using the PHP 5.4 development server, > I had to do some extra work to find out where my php.ini file was located.
    Open up phpinfo again and check for the "Loaded Configuration File" entry. When you do this, make sure you access phpinfo as served by the 5.4 development server. If you access a phpinfo served through Apache2, it may not be the same file. For me, the correct file > was the default /etc/php5/cli/php.ini.

Add these lines to your php.ini to turn on remote debugging and remote autostart.網絡

xdebug.remote_enable=On
xdebug.remote_autostart=On測試

  1. Now that you've got all of that set up, you can give vim and xdebug a try. Open a file in vim and press F5 to initiate the connection. Then go to your browser and navigate to a page that is being served by the PHP 5.4 development server and go back to vim. If all works well, you should see that a connection has been established. Press enter to begin debugging.

按照他的步驟,第一步安裝了 php-xdebug 模塊,第二步我使用的是 VSCode 的 xdebug 插件,該插件與 xdebug 進行通訊,第三步要修改 php.ini,我打開內置服務器上的 phpinfo() 輸出頁面,找到加載的 php.ini 文件路徑:/etc/php/7.0/cli/php.ini,打開 php.ini 添加了兩行配置:網站

xdebug.remote_enable=1
xdebug.remote_autostart=1

保存了以後重啓內置PHP服務器。xDebug 就算配置好了。ui

而後在 VSCode 添加一個調試配置,就像以前調試 Apache Web 網站同樣,打開對 xDebug 端口的監聽:
this

test.php 頁面設置斷點後,打開瀏覽器,訪問:http://localhost:4000/test.php ,就能自動運行到斷點處了。

至此,已經成功地調試內置PHP服務器中的代碼。

可是我天然地想到,這裏沒有 Apache Web 站點,也能成功調試,緣由很顯然是安裝了 php-xdebug 擴展,修改了 php.ini 的配置,等等!,修改了 php.ini 的配置!這個 php.ini 的配置是 CLI 下PHP的配置,那麼,也就是說,直接命令行執行PHP腳本文件應該也能調試。

因而在 VSCode 中增長了調試單個文件的配置:在 .vscode/launch.json 中增長下面代碼:

"configurations": [
        {
            "name": "Launch currently open script",
            "type": "php",
            "request": "launch",
            "program": "${file}",
            "cwd": "${fileDirname}",
            "port": 9000
        }
    ]

這個配置就是 VSCode 的 xDebug 插件的默認生成的調試PHP單文件的配置,其插件文檔裏面有說明,早就看到了,可是並不能調試,一點調試按鈕就運行完了。也就是說,中間缺乏了某些東西,這些東西可以讓 xDebug 插件捕獲到 xDebug 調試進程。如今,增長了 CLI版本的 php.ini 的配置之後,一點調試,果然成功運行到斷點。

因此最終實現了,打開一個PHP文件,直接 F5 開啓調試,腳本就能運行到斷點了。實現了一鍵調試PHP單文件腳本。

PS: 我的博客連接:使用 VSCode 調試單個 PHP 文件

相關文章
相關標籤/搜索