Laradock 使用 PhpStorm Debug 程式碼

Mortence發表於2019-02-21

安裝 Xdebug 擴充套件

  • 編輯 laradock 目錄下 .env 檔案,修改以下兩項
    WORKSPACE_INSTALL_XDEBUG=true
    PHP_FPM_INSTALL_XDEBUG=true
  • 修改後執行 docker-compose build php-fpm workspace 使配置生效

配置 Xdebug

  • 編輯 laradock/workspace/xdebug.ini 及 laradock/php-fpm/xdebug.ini,具體配置如下

    xdebug.remote_host=docker.for.win.localhost
    xdebug.remote_connect_back=0
    xdebug.remote_port=9000
    xdebug.idekey=PHPSTORM
    
    xdebug.remote_autostart=0
    xdebug.remote_enable=0
    xdebug.cli_color=0
    xdebug.profiler_enable=0
    xdebug.profiler_output_dir="~/xdebug/phpstorm/tmp/profiling"
    
    xdebug.remote_handler=dbgp
    xdebug.remote_mode=req
    
    xdebug.var_display_max_children=-1
    xdebug.var_display_max_data=-1
    xdebug.var_display_max_depth=-1

    其中 remote_host 很關鍵。我使用的是 windown,如使用 Mac 應修改為 docker.for.mac.localhost

    配置 PhpStorm

  • 配置 PHP server,具體配置如圖
    file
    其中server name 需和 laradock 目錄中 .env 檔案中 PHP_IDE_CONFIG 配置項一致,預設為 laradock

  • 配置、驗證 Xdebug
    • 配置保持預設配置即可,修改完配置後需校驗配置
      file
    • 點選 Validate 後介面如圖
      file
      點選下方 Validate 按鈕,校驗成功後如圖
      file
      填寫校驗指令碼地址時,需注意,如專案為 laravel ,目錄必須為 public 目錄。在這一步我被坑了很久,直接填寫專案目錄校驗會 404
    • 新增 debug 專案配置
      run -> Edit Configurations,新增 PHP Remote Debug。其中 server 為 laradock 的 PHP_IDE_CONFIG 配置項,預設為 laradock,IDE key 為 PhpStorm。如圖:
      file
    • 配置完成後,選擇上一步配置的 remote debug 啟功,設定斷點,重新整理瀏覽器專案網頁,即可開始除錯

相關文章