Xdebug Laravel artisan命令

19

我经常使用xdebug来调试应用程序。我建立了一个laravel应用程序,它接受csv上传并将数据插入到数据库和作业队列中。

我编写了一个artisan命令,并通过cron计划运行来处理这些数据。

Xdebug在通过浏览器访问站点时有效,但是从命令行运行时无法在断点处停止。

我运行php5-fpm。我的文件/etc/php5/fpm/php.ini/etc/php5/cli/php/ini都包含以下设置:

zend_extension=/usr/lib/php5/20121212/xdebug.so 
xdebug.remote_enable = 1 
xdebug.idekey = 'dev_docker' 
xdebug.remote_autostart = 1 
xdebug.remote_connect_back = {{my host ip}} 
xdebug.remote_port = 9000 
xdebug.remote_handler=dbgp

然后我运行了artisan命令

php artisan jobqueue::process --batch-size=10 --sleep=10

我知道该命令正在运行,因为“->info('text')”显示在终端中。

有人知道我漏掉了什么吗?


这个相关的问题对我很有帮助:https://dev59.com/sl4c5IYBdhLWcg3wa52M - Ryan
我曾经也遇到同样的问题,花了超过一小时才解决。这是一个可行的解决方案:https://dev59.com/pnI-5IYBdhLWcg3wQV4Z#61865143 - 8ctopus
8个回答

37

也许这会对某些人有所帮助。

简而言之,我遇到了同样的问题,但是采纳的答案没有起作用。我的解决方案是从命令行运行以下内容:

php -dxdebug.remote_enable=1 -dxdebug.remote_autostart=on -dxdebug.remote_mode=req -dxdebug.remote_port=9000 -dxdebug.remote_host=127.0.0.1 artisan my:command

1
这对我没有用。Netbeans仍然只显示“运行”,并没有在标记的行处停止。 - Ryan
3
你应该添加 dxdebug.remote_autostart=on - Dragas
嗨,Boroboris,你能看看是否能回答这个相关问题吗?https://dev59.com/v7Hma4cB1Zd3GeqPSeEE - Ryan
1
这应该被标记为正确答案。xdebug.remote_autostart=on 就是那个。 - jayarjo
这对我在Windows(XAMPP)上有效,但我必须在每个“-d”选项后面加上一个空格才能使命令正常工作:php -d xdebug.remote_enable=1 -d xdebug.remote_autostart=on -d xdebug.remote_mode=req -d xdebug.remote_port=9001 -d xdebug.remote_host=127.0.0.1 -f artisan my:command - MarijnK
1
如果使用 docker: -dxdebug.remote_host=host.docker.internal - Nickson Yap

13

如果您使用的是 XDebug 3 版本,请尝试:

php -dxdebug.mode=debug -dxdebug.client_host=host.docker.internal -dxdebug.client_port=9003 -dxdebug.start_with_request=yes artisan your:command

更新 2023-08

我正在使用 xDebug v3.2.1,php v8.2。如果你不想每次都写那些冗长的命令,请将以下内容添加到 xdebug.ini 文件中。 /etc/php/8.2/modes-available/xdebug.ini

zend_extension=xdebug.so
xdebug.mode=debug
xdebug.client_host=127.0.0.1
xdebug.client_port=9003
xdebug.start_with_request=yes

7
这就是我所需要的:php -dxdebug.mode=debug -dxdebug.start_with_request=yes artisan my:command。 - Johan Fredrik Varen

3

我已经通过remote_autostart=1的方式,以及将PHP_IDE_CONFIG环境变量设置为"serverName=localhost"来实现了功能。其中localhost是PHPStorm中服务器配置的名称。现在当我运行php artisan命令时,可以在常规断点处进行调试。

让我更加明确:

如果你已经使用PHPStorm和常规请求使xdebug正常工作了,那么你需要这样做才能让它在命令行php(artisan)中正常工作。

你已经在PHPStorm中配置了路径,所以它知道应该在哪个文件上显示断点。这些路径在服务器下进行配置(首选项->语言和框架->PHP->服务器)。

这个服务器的名称应该是PHP_IDE_CONFIG环境变量中的serverName值。


终于啊!我一直在使用正确的xdebug配置,但是xdebug总是失败。问题是我需要在phpstorm中设置正确的端口和服务器配置,以使xdebug运行。 - Hilman Nihri

3
如果您使用vagrant,那么您可以创建artisandebug shell文件。
#!/bin/bash
HOST=10.0.2.2

# xdebug 3
php -dxdebug.mode=debug -dxdebug.start_with_request=yes -dxdebug.client_host=$HOST -dxdebug.client_port=9003 artisan  "$@"

# xdebug < 3
# php -dxdebug.remote_autostart=on -dxdebug.remote_connect_back=off -dxdebug.remote_host=$HOST -dxdebug.client_port=9003 artisan  "$@"

将其设置为可执行文件并运行命令:

chmod +x artisandebug

./artisandebug some:command --there

1

嗨Furgas,你能否看一下是否能回答这个相关的问题?https://dev59.com/v7Hma4cB1Zd3GeqPSeEE - Ryan

0

用于 xdebug 3.1.1 和 docker 的性能分析

php 
-dxdebug.mode=profile 
-dxdebug.client_host=IP_SERVICE_WITH_PHP 
-dxdebug.client_port=XDEBUG_CLINT_PORT 
-dxdebug.start_with_request=yes 
-dxdebug.output_dir=/tmp 
artisan ARTISAN_COMMAND

例子

php -dxdebug.mode=profile -dxdebug.client_host=172.19.0.3 -dxdebug.client_port=9003 -dxdebug.start_with_request=yes -dxdebug.output_dir=/tmp artisan help

0

另一种方法是:

  1. 首先启动调试器
  2. 运行:php artisan tinker
  3. 在 tinker 中运行:Artisan::call('your-command');

0
php -dxdebug.mode=debug -dxdebug.client_host=localhost -dxdebug.client_port=9003 -dxdebug.start_with_request=yes artisan ARTISAN_COMMAND

1
请在您的答案中添加一些解释,以便其他人可以从中学习。 - Nico Haase

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接