16
13

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

VSCodeでxdebugを利用する on ローカル開発 with Laravelプロジェクト

Posted at

「xdebugがうまく使えないんですけど」と、2人中2人に聞かれたので、記事にまとめておくことにしました。

環境

全部同じマシン上です!

とにかく動かしたい!!(設定)

xdebugのインストール

# pecl install xdebug

php.ini の修正

# フルパスでかく!
zend_extension="/usr/local/php/modules/xdebug.so"

## xdebugの設定
xdebug.remote_enable=1
xdebug.remote_autostart=1

phpinfoで、xdebugが有効か確認できる模様

<? phpinfo() >

VSCode にxdebugクライアント(Extension)をインストール

  • "PHP Debug" をExtensionで検索してインストール

VSCode の設定

  • VSCodeの"Debug"で、"Add Configuration"を選択し、"PHP"を選択

php.iniで特に設定していないので、ここも設定しない

デバッグ実行

VSCodeでデバッガ起動

  • VSCodeの"Debug"で、設定したConfigurationを選択する → クライアントが起動

PHPサーバを立ち上げ

$ php artisan serve

説明

VSCodeのlaunch.json で、

launch.json
    "configurations": [
        {
            "name": "Listen for XDebug",
            "type": "php",
            "request": "launch",
            "port": 9000
        }
・・・

と書いてあるせいか、ブラウザでhttp://localhost:9000/に接続し、「動きません」と2回中2回言われたのですが、ポート9000は、デバッガ(xdebugクライアント)のものなので、ブラウザで接続するのは誤りです。

処理の説明は、Communication Set-up で丁寧に図示されていますが、

  1. PHPを起動する(大抵はブラウザからリクエストを飛ばす。もちろんartisanコマンドでもOK)
  2. PHP(のxdebug.so)が、設定に基づきクライアントへ接続する(デフォルトではローカルの9000. DBGPと言うらしい)
  3. xdebugクライアントが応答し、デバッグ処理が行われる(ブレイクポイントとか)
  4. PHPが結果を返す
    と言う流れとなります。

おわりに

laravelに特化して書きましたが、普通のPHPの説明となります。

16
13
2

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
16
13

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?