用户
 找回密码
 入住 CI 中国社区
搜索
查看: 7151|回复: 0
收起左侧

PHPDesigner7 下用XDebug 调试CI代码的解决方案

[复制链接]
发表于 2011-2-16 23:26:17 | 显示全部楼层 |阅读模式
今天折腾了一天这个问题,终于搞通了。走了不少弯路,查了很多资料,写点心得体会。
系统环境: WinXP ,  Apache2.2.4  PHP5.2.17 ,  Xdebug v2.1.0 , PHPDesigner7.2.4

1. 下载 XDebug ( http://www.xdebug.org/find-binary.php  ) ,打开这个页面,填入你的 phpinfo()输出,他会提示你下载合适的binary和需要添加到 php.ini里面的内容。

我的添加的内容是:
[XDebug]
zend_extension_ts = "C:\PHP5.2.17\ext\php_xdebug-2.1.0-5.2-vc6.dll"
xdebug.remote_enable=true
xdebug.remote_host=127.0.0.1
xdebug.remote_port=9000
xdebug.remote_handler=dbgp
xdebug.profiler_enable=1
xdebug.dump.*
xdebug.profiler_enable_trigger=on
xdebug.profiler_output_dir=C:\Documents and Settings\All Users\Application Data\phpDesigner\XDebugCache

2. 重启apache以后,刷新phpinfo() 页面,确认看到以下内容:

This program makes use of the Zend Scripting Language Engine:
Zend Engine v2.2.0, Copyright (c) 1998-2010 Zend Technologies
    with Xdebug v2.1.0, Copyright (c) 2002-2010, by Derick Rethans

3. 配置 PHPDesigner, 菜单 - tools - pereference - debugger ,把  PHP 设置成你的php-cgi.exe, configuration也指向你的ini 文件

这里有个问题,如果你的PHP是安装成 apache2 module的话,你的PHP目录下面只有 php.exe 和php-win.exe,没有 php-cgi.exe ,但是这里
必须要 php-cgi.exe ,我的办法是重新运行php的原始安装包,点CHANGE然后选择 Apache CGI模式,安装成功后你就有了php-cgi.exe,这是你需要
把这个文件备份出来,然后再次运行安装包,改回成 Apache module安装,这时候你会发现安装目录下的php-cgi.exe又不见了,好在我们有备份,现在把
刚才的备份文件复制回来。现在你的PHP目录下有3个执行文件了:

C:\PHP5.2.17>dir php*.exe
Volume in drive C has no label.
Volume Serial Number is 18A3-CBD9

Directory of C:\PHP5.2.17

2011-02-16  15:50            49,230 php-cgi.exe
2011-01-06  17:30            32,846 php-win.exe
2011-01-06  17:30            32,842 php.exe
               3 File(s)        114,918 bytes
               0 Dir(s)  22,054,506,496 bytes free

4.  配置 PHPDesigner, 菜单 - tools - pereference - debugger - Run下面也配置成和上面一样

5. 配置 PHPDesigner, 菜单 - tools - pereference - debugger - localhost 配置你的local apache server的参数

server path :  http://localhost:8753/CodeIgniter/     # 我的代码在CodeIgniter这个虚拟目录下,端口也不是标准的80
local server path : C:\CodeIgniter_2.0.0\                  # 这是我的代码实际路径
port :  []                                                                      #不填,因为我已经填在第一步了,如果我第一步不填而放这里的话,最后拼出来的地址会是 http://localhost/CodeIngiter:8753  那就不对了

6. 现在你可以打开根目录下的 index.php文件,然后按F9试试看, 你会发现得不到正确的页面,而是出现一个错误提示:

Disallowed Key Characters.
这是因为PHPDesigner 调试的时候路径是windows的实际文件路径,如 C:\CodeIgniter\index.php,而  \ 在CI里面被识别为非法字符

打开 system/core/Input.php,找到 function _clean_input_keys($str),注释掉以下代码
*                 if ( ! preg_match("/^[a-z0-9:_\/\\-]+$/i", $str))
*                 {
*                         exit('Disallowed Key Characters.');
*                 }

换成
   $config = &get_config('config');
    if ( ! preg_match("/^[".$config['permitted_uri_chars']."]+$/i", rawurlencode($str)))
    {
        exit('Disallowed Key Characters.');
    }

7. 到这里,你已经可以在index.php文件里面设置断点,单步运行了,可是如何运行指定的controller呢,点击 "Debug with Parameters",填入 c=control_name&m=method&other=...

当然,你需要在config 里面设置:
$config['allow_get_array']                = TRUE;
$config['enable_query_strings'] = TRUE;
$config['controller_trigger']        = 'c';
$config['function_trigger']                = 'm';
$config['directory_trigger']        = 'd';  // experimental not currently in use

这相当于是运行 index.php/control_name/method/other/...

如果你的control是在子目录下的,如  path1/myclass.php,那个Parameter需要写成  d=path1&c=myclass&m=method&others=....

CI默认只能有一层目录,需要多层目录支持的请看HEX的另外一个帖子 http://codeigniter.org.cn/forums/thread-2849-1-1.html

好了,现在去你的controller文件里面设置一个断点,在回到 index.php,运行下试试看吧 (注意:每一次运行必须是回到index.php文件才能开始 )

7. 说到这里基本上结束了,还有一点提醒下,如果你有一个后台的ajax文件,例如 application/controllers/backend/read_db.php ,当然企图运行前台页面,点击一个事件去出发这个后天程序的时候,调试器是不可能进入到这个后台程序的,事实上你前台的调试进程这时候已经结束了。要调试这个后天程序,你只能把他当成前台程序直接去运行,并且提供模拟输入,例如 Debug参数写 : d=backend&c=read_db&(...模拟数据输入...) 。再提醒下,启动Debug运行的时候必须先回到index.php页面


第一次写PHP的心得,错误难免,欢迎交流 (事实上我学PHP还不到一个月,呵呵,见笑)

本版积分规则