在开发中选好一款实用的开发工具。其实,对我们的开发效率至关重要。相信大家对sublime text非常熟悉,之前我一直都是使用这款编辑器,但因为这款编辑器对代码调试非常不方便,身为PHP轮子者,之前一直用var_dump等打印进行调试,这样调试起来效率非常低。本次跟大家分享的是用于PHP开发的一款PHPStorm(IDE)编辑器,我也是最近开始使用,更换编辑器的原因是它对debug非常方便。接下来就展示如何配置调试环境
确定xdebug是否安装
~ $ php -v
PHP 5.6.21 (cli) (built: Apr 29 2016 02:31:44)
Copyright (c) 1997-2016 The PHP Group
Zend Engine v2.6.0, Copyright (c) 1998-2016 Zend Technologies
with Xdebug v2.4.0, Copyright (c) 2002-2016, by Derick Rethans
如果没有,安装xdebug
~ $ brew install php56-xdebug // mac上可以通过homebrew安装
//没有homebrew环境的,通过/usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"这条命令安装
~ $ apt-get install php5-xdebug //ubuntu上安装指令
~ $ php -v //确认是否安装xdebug
xdebug配置
~ $ php --ini //找到配置文件
Configuration File (php.ini) Path: /usr/local/etc/php/5.6
Loaded Configuration File: /usr/local/etc/php/5.6/php.ini
Scan for additional .ini files in: /usr/local/etc/php/5.6/conf.d
Additional .ini files parsed: /usr/local/etc/php/5.6/conf.d/ext-igbinary.ini,
/usr/local/etc/php/5.6/conf.d/ext-mcrypt.ini,
/usr/local/etc/php/5.6/conf.d/ext-memcache.ini,
/usr/local/etc/php/5.6/conf.d/ext-redis.ini,
/usr/local/etc/php/5.6/conf.d/ext-xdebug.ini
~ $ vi /usr/local/etc/php/5.6/conf.d/ext-xdebug.ini //编辑文件
//把下面内容加入ext-xdebug.ini文件
[xdebug]
zend_extension="/usr/local/opt/php56-xdebug/xdebug.so"
xdebug.remote_enable = on
xdebug.remote_host = "localhost"
xdebug.remote_port = 9000
xdebug.remote_handler = "dbgp"
xdebug.idekey = "phpstorm"
//如果用到vagrant虚拟机上的PHP环境,则配置如下
zend_extension=/usr/lib64/php/modules/xdebug.so
xdebug.idekey = "vagrant"
xdebug.remote_enable = 1
xdebug.remote_port = 9000
xdebug.remote_handler=dbgp
xdebug.remote_log="/var/log/xdebug/xdebug.log"
xdebug.remote_connect_back = on
OK,xdebug到此结束
phpStorm配置
phpstorm>Preferences如下图找PHP,点击PHP,设置PHP环境
展开上图中的Debug,选中DBGp Proxy,在红色标记2处设置Debug监听的host,端口,IDE key
接着就是设置Servers,选中Servers,点击新增,接着就是配置,操作及配置如下图
备注:如果是用到vagrant上的环境,则配置server的时候,就需要用到路径映射。在上面这个图中选中User path mapping之后会显示设置,设置如下图所示
configurations设置
新增PHP Web Application
浏览器xdebug插件配置
到这里整个环境就配置完成,接下来我们在代码中设置一个断点,在点击debug按钮,如果下图