使用VSCode調試單個PHP文件


突然發現是可以使用 VSCode 調試單個 PHP 文件的,今天之前一直沒有弄成功,還以為 VSCode 是不能調試單文件呢。這里記錄一下今天這個“突然發現”的過程。

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

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

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 就算配置好了。

然后在 VSCode 添加一個調試配置,就像之前調試 Apache Web 網站一樣,打開對 xDebug 端口的監聽:

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 文件


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM