lundi 3 novembre 2008

Debug symfony / PHP applications with Eclipse + XDebug

Never wanted a PHP debugger as you have with JS debugger firebug ?

It is possible with Eclipse and XDebug php extension... ( thanks to this article )



Stack trace,
breakpoints,
watch expressions,
variables,
...












The steps :

Install Xdebug :

sudo apt-get install php5-xdebug


Then, edit extension's php config :

sudo vi /etc/php5/conf.d/xdebug.ini


And add just after the line 'zend_extension=...' :

xdebug.remote_enable=On
xdebug.remote_host="localhost"
xdebug.remote_port=9000
xdebug.remote_handler="dbgp"
xdebug.max_nesting_level=1000
xdebug.collect_params=2
xdebug.collect_return=On


Then, restart your Apache :

sudo /etc/init.d/apache2 restart



It's time to configure your Eclipse :


You surely will have to configure the Browser to 'external web browser' in your Preferences->General->Web Browser configuration.

use 'Run' menu or 'Debug' toolbar icon to access 'Open Debug Dialog'.

Make a 'New' PHP Web Page configuration

- edit the first tab ( server ) :

  • Choose XDebug as server debugger.
  • Make a new PHP Server if you have a virtualHost ( in my example: http://symfony.me )
  • Set 'File' Field to the absolute path of your front controller ( in my example: /var/www/symfony/web/frontend_dev.php )
  • Uncheck 'Break at first line'
  • Uncheck AutoGenerate URL to make it the same as when you debug your app ( in my example: http://symfony.me/frontend_dev.php/
- edit the second tab ( advanced ) :
  • check Open in browser
  • choose Start Debug from and add (in my example) : http://symfony.me/frontend_dev.php
  • check 'Continue Debug from this page'

That's It !

Now start debugging :

Add a breakpoint by right clicking a line number in your file -> toggle breakpoint.

the PHP debug perspective contains an Expression View where you can add your own watch expressions.

Discover the tool as I'm actually doing :) !

Modify project nature in eclipse

As said here, you can modify the .project file to add your builders, and the nature of the project.

This works for my eclipse PDT 3.3.2


<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
<name>symfony</name>
<comment></comment>
<projects>
</projects>
<buildSpec>
<buildCommand>
<name>org.eclipse.wst.jsdt.core.javascriptValidator</name>
<arguments>
</arguments>
</buildCommand>
<buildCommand>
<name>org.eclipse.php.core.PhpIncrementalProjectBuilder</name>
<arguments>
</arguments>
</buildCommand>
<buildCommand>
<name>org.eclipse.php.core.ValidationManagerWrapper</name>
<arguments>
</arguments>
</buildCommand>
</buildSpec>
<natures>
<nature>org.eclipse.php.core.PHPNature</nature>
<nature>org.eclipse.wst.jsdt.core.jsNature</nature>
</natures>
</projectDescription>