Instruction is intended to be used with NFQ Academy KickStart because it have some helper scripts to update xDebug configuration and start connect to docker containers.
Start your containers (it is safe to rerun even containers are already started):
scripts/start.sh
To test:
docker ps --format '{{.Names}}'
Should print:
php.symfony mysql.symfony nginx.symfony
ifconfig docker0 | grep 'inet addr' | cut -d: -f2 | awk '{print $1}'
It should give something like:
172.17.0.1
For Docker for Mac
,
you can use:
host.docker.internal
Use your computer's LAN IP address (not the one of internet facing router)
./scripts/backend.sh /enable_xdebug.sh 172.17.0.1
Where:
172.17.0.1
should be replaced by your IP address (or the one from previous section)
You should see something like
Executing in PHP container: /enable_xdebug.sh 172.17.0.1 xDebug enabled
Open index.php
and click near line number, so red dot appears:
So execution during debugging could pause on this line.
In the right top corner:
Click on Start listening for PHP Debug Connections
:
Green handset icon means xDebug is enabled.
Open 127.0.0.1:8000:
Your browser will pause waiting response from PHPStorm. Open PHPStorm window.
On the right bottom side of PHPStorm you should see Debug
panel popped up:
If you are setting up debugging for the first time,
there should be error message about nfqKickStartDocker
server.
- Click on
Configure servers
(or viaFile
->Settings
->Language & Frameworks
->PHP
->Servers
)
- Add new server configuration with
+
sign. Enter:- Name:
nfqKickStartDocker
(lower/upper case matters) - Host:
127.0.0.1
- Port:
8000
- Debugger:
Xdebug
Checked
Use path mappings:- Near path of your project in File/Directory column
- Add
/code
in Absolute path on the server column. - Value is saved only when you press Enter (or clicked somewhere else)
- Save settings by clicking
OK
- Name:
- You should see error message gone in
Debug
panel:
- Also you should see current xDebug step marked in blue in the code:
It may be useful to debug MySql connection during migrations or other Symfony console functionality
Symfony console commands are not going through index.php,
so add breakpoint in bin/console
file:
Run in terminal:
./scripts/backend.sh
You should see bash prompt like:
php@5372d0fc92d4:/code$
While still in PHP container, enter:
bin/console debug:router
Console will pause execution until PHPStorms allows to run further:
Debug
panel in PHPStorm have many useful actions (and those have keyboard shortcuts):
Common operations are:
Resume Program
when you found the bug and want to go back to normal executionStop
when there is no point in running program further (E.g. it will still fail)Step Over
to check what is happening when running line by lineStep Into
to go inside function or other structure to debug thereVariables
panel shows values of variables for current line
Read more in official PHPStorm documentation
Usually you do not want to pause for each execution (especially for composer install
and similar operations).
Execute in new terminal:
scripts/backend.sh /disable_xdebug.sh
You should see output similar to:
Executing in PHP container: /disable_xdebug.sh xDebug is disabled
So browser and console calls would not wait for PHPStorm
Stop listening of Debug connections:
Useful for temporary stopping debugging.
- You have only 1 simultaneous connection, but other are pending because of console or AJAX calls.
So it is good practice to have higher number or check tabs in
Debug
panel - You have mistyped IP in PHP container or bad
Server
name inPHP
->Servers
configuration. - You have multiple PHPStorm programs opened, but xDebug listener is enabled on wrong window. Port can be assigned only to single application, so you cannot use 2 programs to receive xDebug calls.
- There is firewall for
9000
port connection - For troubleshooting it is useful to temporary enable
Break at first line in PHP Scripts
- You have older
PHPStorm
version, that do not support newestxDebug
(e.g. first line works, stepping does not) – upgrade yourPHPStorm
to newest version