Debugging code can be the most vital task a developer required to do. Debugging makes it easy to find logical errors in code. NetBeans supports PHP debug using xdebug, but in CakePHP it is hard to make this work straight. This is due to custom, pretty, URL rewriting. However you can enable debugging even with pretty URLs and URL rewriting.
To make this works in simple few steps first make sure that xdebug is installed and working. To make sure it is installed open you php info file or create a new php file and fill it with:
<?php echo phpinfo(); ?>
and open it in your browser and make sure that xdebug appears like this:
If you found xdebug is not installed then you can find many resources on how to install xdebug on Google. If it is already installed then make sure that your xdebug is configured correctly in your php.ini. NetBeans requires xdebug to be configured as follows:
xdebug.remote_enable=on xdebug.remote_handler=dbgp xdebug.remote_host=0.0.0.0 #or your preferred hostname xdebug.remote_port=9000
Now we will start enabling CakePHP debugging:
- First you’ll need to create a new CakePHP application using CakePHP console or use current CakePHP application folder.
- In NetBeans create a new PHP application and select “PHP Application with Existing Sources” and click next.
- Select your application folder and choose a name for you application and click next.
- Write your project base URL and make sure to select index.php as your ‘Index File’ and click finish.
- Go to Projects panel and right click your project and select Properties from the menu on left select Sources then select your Web Root folder, and it is ovious what to select
- Run Configuration and click Advanced. From the dialog select “Ask Every time” and click OK to close the dialog and click OK again to close Project properties dialog.
Now the configurations are complete and you can set break points. To create a break point you can click on the line number on the left of source code or write
xdebug_break(); where you want to stop program execution.
When you click debug icon in NetBeans toolbar, or select Debug project from Debug menu (ctrl + F5), NetBeans will show you a box where you enter the URL you want to debug and it will stop execution of the application, and your browser will not continue loading your page until you click Continue.
Now you can debug all your CakePHP source code as normal PHP applications. You can learn how to debug normal PHP application in NetBeans from this article