laravel 如何在 PHPStorm 中调试 php artisan serve?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/32780258/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 12:24:15  来源:igfitidea点击:

How to debug php artisan serve in PHPStorm?

phplaravelphpstormxdebug

提问by chemitaxis

I am using PHPStorm for develop my PHP web pages. All work fine with my Apache Server, XDebug, and a simple web PHP project. No problem.

我正在使用 PHPStorm 开发我的 PHP 网页。我的 Apache 服务器、XDebug 和一个简单的 Web PHP 项目都可以正常工作。没问题。

But, when I try to debug a Laravel 5.1 Web Project using php artisan serve, I can't debug the breakpoints. It's like the php artisan serveuse another server...

但是,当我尝试使用 调试 Laravel 5.1 Web 项目时php artisan serve,我无法调试断点。这就像php artisan serve使用另一台服务器......

And on my PHPStorm, I always see:

在我的 PHPStorm 上,我总是看到:

Waiting for incoming connection with ide key '(randomNumberHere)'

等待与 ide 键 '(randomNumberHere)' 的传入连接

I have configured all in PHPStorm (enabling remote debug, correct port, etc.), and with "normal" PHP projects all works fine.

我已经在 PHPStorm 中配置了所有内容(启用远程调试、正确端口等),并且使用“正常”PHP 项目一切正常。

Can someone tell me if I need to change something?

有人可以告诉我是否需要更改某些内容吗?

Thanks!

谢谢!

回答by raigu

Debugging using php artisan servedoes not work unless you have enabled debugging in ini file.

php artisan serve除非您在 ini 文件中启用了调试,否则调试 using不起作用。

@Bogdan pointed out the reason. artisan servewill call PHP Built-in Web Server but does not pass on the php command line options (named interpreter options in PHPStorm).

@Bogdan 指出了原因。artisan serve将调用 PHP 内置 Web 服务器,但不会传递 php 命令行选项(在 PHPStorm 中命名为解释器选项)。

ie if you execute from command line:

即如果您从命令行执行:

$ php -dxdebug.remote_enable=1 -dxdebug.remote_mode=req -dxdebug.remote_port=9000 -dxdebug.remote_host=127.0.0.1 artisan serve

Then these options given by -dare not passed to called PHP Built-in Web server. You can see the calling of build-in server here.

然后这些由 给出的选项-d不会传递给被调用的 PHP 内置 Web 服务器。你可以在这里看到内置服务器的调用。

Workaround in PHPStorm is to create Run configuration that calls PHP Build-in Web server directly. Instructions:

PHPStorm 中的解决方法是创建直接调用 PHP 内置 Web 服务器的运行配置。指示:

  1. Open Run -> Edit Configurations...
  2. Create new 'PHP Build-in Web Server'
  3. Set values:

    • Host: localhost
    • Port: 8000
    • Document root: select Laravel's public catalog
    • Check Use route scriptand select server.phpin Laravel projects root directory.
    • Interpreter options: -dxdebug.remote_enable=1 -dxdebug.remote_mode=req -dxdebug.remote_port=9000 -dxdebug.remote_host=127.0.0.1
  4. OK and run.

  1. 打开运行 -> 编辑配置...
  2. 创建新的“PHP 内置 Web 服务器”
  3. 设定值:

    • 主持人: localhost
    • 港口: 8000
    • 文档根目录:选择 Laravel 的公共目录
    • 在 Laravel 项目根目录中勾选Use route script并选择server.php
    • 口译选项: -dxdebug.remote_enable=1 -dxdebug.remote_mode=req -dxdebug.remote_port=9000 -dxdebug.remote_host=127.0.0.1
  4. 好,跑。

Now the PHPStorm will execute same command as php artisan servedoes with additional interpreter options. Actually the php artisan serveonly purpose is to append the server.php to PHP Build-In Web Server. server.php just emulates Apache's mod_rewritefunctionality.

现在 PHPStorm 将执行php artisan serve与附加解释器选项相同的命令。实际上php artisan serve唯一的目的是将 server.php 附加到 PHP Build-In Web Server。server.php 只是模拟 Apache 的mod_rewrite功能。

Update: Good reminder from @attila-szeremi: make sure "Start Listening for PHP Debug Connections" is enabled which you manually need to do if you don't run a PhpStorm configuration with "Debug"

更新:来自@attila-szeremi 的好提醒:确保“开始侦听 PHP 调试连接”已启用,如果您不使用“调试”运行 PhpStorm 配置,则需要手动执行此操作

回答by Josh

I don't use phpstorm, but perhaps the solution that I use for debugging in netbeans will prove useful.

我不使用 phpstorm,但也许我用于在 netbeans 中进行调试的解决方案会被证明是有用的。

artisan serve uses a different ini file from the one loaded by your web container

artisan serve 使用与您的 Web 容器加载的 ini 文件不同的 ini 文件

Find this by typing

通过输入找到这个

php --ini

On my ubuntu box it's located at

在我的 ubuntu 盒子上,它位于

Loaded Configuration File:         /etc/php/7.0/cli/php.ini

Edit the ini for your cli environment and use the same configuration you used to enable it for your web container;

为您的 cli 环境编辑 ini 并使用您用于为 Web 容器启用它的相同配置;

Example...

例子...

[Zend]
zend_extension=/usr/lib/php/20151012/xdebug.so
xdebug.remote_enable=1
xdebug.remote_handler=dbgp
xdebug.remote_mode=req
xdebug.remote_host=127.0.0.1
xdebug.remote_port=9000

The only caveat for this, is that as long as you have this configured, it will impact other things that you use php cli for.

唯一需要注意的是,只要你配置了这个,它就会影响你使用 php cli 的其他事情。

Additional note

附加说明

If you want your debug session to always start automatically (instead of initiating a remote debug via URL request parameter XDEBUG_SESSION_START=name, for example, when debugging CLI stuff), you can set XDEBUG to always start a remote debugging session with this additional configuration;

如果您希望调试会话始终自动启动(而不是通过 URL 请求参数 XDEBUG_SESSION_START=name 启动远程调试,例如,在调试 CLI 内容时),您可以将 XDEBUG 设置为始终使用此附加配置启动远程调试会话;

xdebug.remote_autostart = 1

See https://xdebug.org/docs/all

https://xdebug.org/docs/all

Normally you need to use a specific HTTP GET/POST variable to start remote debugging (see Remote Debugging). When this setting is set to 1, Xdebug will always attempt to start a remote debugging session and try to connect to a client, even if the GET/POST/COOKIE variable was not present.

通常,您需要使用特定的 HTTP GET/POST 变量来启动远程调试(请参阅远程调试)。当此设置设置为 1 时,Xdebug 将始终尝试启动远程调试会话并尝试连接到客户端,即使 GET/POST/COOKIE 变量不存在。

回答by Anup Dhakal

So, after going through Jeffry's Be Awesome in PhpStorm, I had been stuck in a similar situation to the OP (in the chapter regarding Xdebug and Laravel). I was setting up the breakpoints, but PhpStorm was not able to break the execution according to those points. And, to be honest, Josh's answer here somewhat helped me to understand the problem, but was not clear enough for me. So, I went out and found the solution myself by hit and trial method, the final solution of which I want to share. May be it will come in handy for some folks still wondering.

因此,在完成了 Jeffry 的Be Awesome in PhpStorm 之后,我陷入了与 OP 类似的情况(在有关 Xdebug 和 Laravel 的章节中)。我正在设置断点,但 PhpStorm 无法根据这些点中断执行。而且,说实话,乔希在这里的回答在某种程度上帮助我理解了这个问题,但对我来说还不够清楚。于是,我自己出去通过hit和trial的方法找到了解决方案,最终的解决方案我想分享一下。也许它会派上用场,有些人仍然想知道。

Firstly, as OP said/guessed, php artisan serverdoesuse another server. It is PHP's built in web server plusthe server.phprouter file. And the configurations it uses are the CLI configurations. Try php --inito find all the included configurations.

首先,正如 OP 所说/猜测的那样,php artisan server确实使用了另一台服务器。这是PHP的内置Web服务器加上server.php路由器文件。它使用的配置是 CLI 配置。尝试php --ini找到所有包含的配置。

In my case (Ubuntu 17.10), the Xdebug configuration file being included was from the location /etc/php/7.2/cli/conf.d/20-xdebug.ini. I added the second line to enable remote xdebug and now the file looks like this with total 2 lines:

在我的情况下(Ubuntu 17.10),包含的 Xdebug 配置文件来自位置/etc/php/7.2/cli/conf.d/20-xdebug.ini. 我添加了第二行来启用远程 xdebug,现在文件看起来像这样,总共有 2 行:

zend_extension=xdebug.so xdebug.remote_enable=1

zend_extension=xdebug.so xdebug.remote_enable=1

Now for configuring PhpStorm:

现在配置 PhpStorm:

  1. Run> Edit Configurationsand give any name or just leave it as Unnamed
  2. Click ...after the Server:option
  3. Click +and give any name (for example Laravel App)
  4. Set Hostas localhost, Portas 8000and Debuggeras Xdebugand leave any other check marks unchecked!
  5. Click Apply/ OK
  6. From the drop down list of Server:select the one we just set up.
  7. Set the Start URL:the URL where you want to start debuging (for example \user)
  8. Select the browser you prefer.
  9. Click Apply/ OK
  1. Run>Edit Configurations并给出任何名称或将其保留为未命名
  2. 点击...服务器:选项
  3. 单击+并给出任何名称(例如Laravel App
  4. 设置Hostas localhostPortas8000DebuggerasXdebug并且不选中任何其他复选标记!
  5. 点击Apply/OK
  6. Server下拉列表中选择我们刚刚设置的那个。
  7. 设置开始 URL:要开始调试的 URL(例如\user
  8. 选择您喜欢的浏览器。
  9. 点击Apply/OK

Please remember that the debugger will only work after the actual web server starts, which is the server that starts with php artisan servercommand.

请记住,调试器只有在实际的 web 服务器启动后才会工作,即以php artisan servercommand启动的服务器。

Now, start the web server ie. php artisan serveand in PhpStorm click Run> Debug 'your-debug-config', and everything should work fine!

现在,启动 Web 服务器,即。php artisan serve并在 PhpStorm 中单击Run> Debug 'your-debug-config',一切正常!

回答by lewis4u

Part 1 Run > Edit Configurations

第 1 部分运行 > 编辑配置

choose PHP-BuiltIn Web Server and just set the Host: localhost Port: 8000 // or whichever you are using

选择 PHP-BuiltIn Web Server 并设置 Host: localhost Port: 8000 // 或任何你使用的

enter image description here

在此处输入图片说明

Click OK

单击确定

Part 2 Install Xdebug helper Chrome extension

第 2 部分安装 Xdebug 助手 Chrome 扩展

enter image description here

在此处输入图片说明

In Chrome browser there should be a bug icon in top right corner Click on it and choose Debug so that the gray bug icon changes the color to green

在 Chrome 浏览器中,右上角应该有一个错误图标单击它并选择调试,以便灰色的错误图标将颜色更改为绿色

enter image description here

在此处输入图片说明

Now when you run php artisan serve and set a debug checkpoint

现在,当您运行 php artisan serve 并设置调试检查点时

enter image description here

在此处输入图片说明

and click on phone icon to "Start listening for Xdebug connection"

并单击电话图标以“开始侦听 Xdebug 连接”

enter image description here

在此处输入图片说明

it should catch your debug checkpoint

它应该捕获您的调试检查点