Laravel, Valet, and XDebug

I’ve been exploring Laravel. Valet made it easy to get up and running quickly on my MacBook and I wrote a simple RESTful api (reminder).

Then I wanted to debug one of the endpoints…

Cool, that’s a job for XDebug. XDebug is an important part of my php toolset but I don’t leave it installed all the time because it can really slow down other php based tools that I use, like Composer. I have a couple of functions in my .zshrc that let me enable and disable XDebug as needed:

xdon() {
 sed -i '' 's/;zend_extension/zend_extension/' /usr/local/etc/php/7.1/conf.d/ext-xdebug.ini
 grep zend_extension /usr/local/etc/php/7.1/conf.d/ext-xdebug.ini
 export XDEBUG_CONFIG="idekey=PHPSTORM remote_host=127.0.0.1 remote_port=9001"
}

xdoff() {
 sed -i '' 's/^zend_extension/;zend_extension/' /usr/local/etc/php/7.1/conf.d/ext-xdebug.ini
 grep zend_extension /usr/local/etc/php/7.1/conf.d/ext-xdebug.ini
 unset XDEBUG_CONFIG
}

I’m not going to go in depth about what these functions do (that’s another blog post). But I expected to be able to set a breakpoint in PhpStorm and then, in a shell, type:

xdon
curl 'http://reminder.dev/api?XDEBUG_SESSION_START=PHPSTORM'

Nope, didn’t work. So I did the dance of checking all the things:

  • PhpStorm listening for debug? check
  • Using port 9001? check
  • PhpStorm directory mappings? check
  • search the web… this

Long story short: Valet sets your Mac up to be an nginx webserver delegating web requests to php-fpm. But php-fpm read my php.ini config files long before shell command edited them to enable XDebug. Quick solution:

valet restart

And I’m a happy man 🙂

Comments are closed.