LoginSignup
1
0

More than 1 year has passed since last update.

PHPのビルトインサーバでXdebugを使いVSCodeでデバッグする

Last updated at Posted at 2023-01-21

Install

php.ini

; php --ini でphp.iniもしくはxdebug.iniを探してください

zend_extension = "xdebug.so"
xdebug.client_host = 127.0.0.1
xdebug.client_port = "9003"
xdebug.mode = debug
xdebug.idekey = "PHPSTORM"
xdebug.start_with_request = yes

PHPビルトインサーバを起動する

  • プロジェクトによりけりだと思いますが、下記のようなphpコマンドで起動します。
    • -S でビルトインサーバを起動します
    • -t でドキュメントルートを指定します
    • 引数でルーティングファイルを指定します
# 例
php -S 127.0.0.1:8081 -t public router.php

# もしくは某弊社のよくあるパターンならこんな感じです
php -S 127.0.0.1:8081 -t public bin/content.php

VSCode設定 launch.json

  • VSCodeの実行とデバッグで、設定ファイルを作成するか、既存の設定があるならそれを編集します。
  • runtimeExecutableでphpのpathを設定します。
  • programでphpunitのpathを設定します
{
  // Use IntelliSense to learn about possible attributes.
  // Hover to view descriptions of existing attributes.
  // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
  "version": "0.2.0",
  "configurations": [
    {
      "name": "Listen for Xdebug(PHPビルトインサーバ)",
      "type": "php",
      "request": "launch",
      "port": 9003
    },
    {
      "name": "Launch currently open script(PHP CLI)",
      "type": "php",
      "request": "launch",
      "cwd": "${fileDirname}",
      "runtimeExecutable": "${userHome}/.phpenv/shims/php",
      "runtimeArgs": [
        "-dzend_extension=xdebug.so"
      ],
      "program": "${workspaceRoot}/vendor/bin/phpunit",
      "args": [
        "${file}"
      ],
      "port": 9003,
      "log": false,
      "env": {
        "XDEBUG_MODE": "debug,develop",
        "XDEBUG_CONFIG": "client_port=${port}"
      }
    }
  ]
}

VSCodeでXdebugと接続する

  • VSCodeで、Listen for Xdebug(PHPビルトインサーバ)を選択して、 ▶️ で実行します。

Screenshot 2023-01-21 at 15.34.33.png

VSCodeでデバッグしたいファイルにブレークポイントを設定します

  • たとえばTOPページをデバッグしたいならindex.phpなどのphpファイルをVSCodeで開き、ブレークポイントを設定します。

Screenshot 2023-01-21 at 15.53.49.png

ブラウザでデバッグしたいページにアクセスする

  • ここで今回の例であれば、TOPページPHPファイルをホストしているのが、上で設定したPHPビルトインサーバです。
  • 一連の設定がうまく行っていれば、ブラウザでアクセスするとフォーカスがブラウザからVSCodeに移り、ブレークポイントを設定したindex.phpファイルで処理が止まります。
  • ブレークポイントで、変数をウォッチしたり、出力したり、関数を実行したり、処理の中へステップインしたりといったデバッグが可能です。

Screenshot 2023-01-21 at 15.47.19.png

常時Xdebug onでも遅くはないですね

  • off:778ms => on:1.59s くらいでした。
  • 個人的にはこれくらいなら常時xdebug onでも我慢できそうです。

Screenshot 2023-01-21 at 16.25.57.png

1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0