After I recently upgraded to Zend Studio for Eclipse, I noticed one troubling feature of my setup – I couldn’t debug applications with “pretty urls”. Zend Studio for Eclipse has a great debugging platform setup, even with a dedicated IDE layout for debugging, and the Zend Studio Debugger backend on the server is quite powerful in itself. In this tutorial, I’ll show you how to make use of that power and debug your Cake applications with Zend Studio for Eclipse.
The problem with debugging pretty URLs, is that the debugging system relies on the debugger client (your IDE) starting a debugging session with the debugger server (the php extension running on your server) by sending some data through the URL. Here’s a sample of these URLs:
…/cake/controller/?debug_session_id=1000 &start_debug=1 &debug_start_session=1 &isDebugURL=1 &original_url=http://localhost:8080/carbooking/cars/
Not terribly pretty, and certainly not going to pass for your application. The main issue here is that the .htaccess file with the mod_rewrite instructions will either remove this debug URL data before it gets to PHP, or send it on incorrectly, preventing your server from establishing the debugging connection with your client. It can also wreak havoc with your rewriting in the first place; with other frameworks especially, you’d be lucky to even see the actual page with a URL like this.
However, we can get our debugger working with CakePHP applications relatively easily. Presuming your regular debugger is working just fine (try debugging a simple echo “Hello World” script in your docroot), we can disable the mod_rewrite rules and tell Cake to use not-so-pretty URLs to get debugging working fine. These instructions are written for CakePHP 1.1 stable, but should work just fine on 1.2 beta.
1. Disable all .htaccess files
Cake actually ships with three
.htaccess files; one in the root cake install dir, one in
/app, and one in
/app/webroot. Find all three, and thoroughly rename them. Be careful of minor alterations to names, such as changing
..htaccess (additional full stop) – you can actually crash your Apache server this way. I’m changing all three to
2. Add “index.php” to pretty URLs
The most common way frameworks handle pretty URLs is by using Apache’s mod_rewrite to translate them to /somefile.php/some/url/parameter format. Normally, however, you would only see the /some/url/parameter format – mod_rewrite does the magic behind the scenes. By putting this information after the .php filename, it is easily accessible within PHP. We want to avoid mod_rewrite messing up our URLs, so we’re going to revert to this format, making our Cake URLs look like /index.php/controller/action/parameter etc.
Open up app/config/core.php. Find this line:
// define ('BASE_URL', env('SCRIPT_NAME'));
Uncomment this line by removing the //. (In Zend Studio for Eclipse, the syntax highlighting will reappear on the line.)
3. Set a breakpoint to debug
Open up one of your controllers, and double click the blue margin (left of the line numbers) on a line of code in your index action. Alternatively, right click anywhere in the margin of the line and click “Toggle breakpoint”. A blue dot will appear on that line in the margin – this represents a breakpoint, or a point where the debugger will temporarily pause and allow you to take a look at how the script is going.
4. Debug your application using /index.php/ URLs
In Zend Studio for Eclipse, click the “Debug URL” button – it’s to the right of the four debug and run buttons in the default toolbar of the PHP perspective, and its also the second-last item in the “Run” menu. Enter the URL to your controller running on your local server, and make sure you include the
index.php/ section. For example, I’m entering http://localhost/cake/index.php/cars/ for the
CarsController::index() action in my Cake installation under
docroot/cake. “Break at first line” can remain checked.
When you click Ok to continue, Zend Studio will contact the server, work out which file you are starting at – probably /app/webroot.index.php – and pause on the first line of code (the line may be highlighted yellow). In your Debug pane (in the “PHP Debug” perspective – Zend Studio for Eclipse should automatically switch, but if not, use the arrow in the top right of the IDE) you’ll see a yellow “resume” button. This is how you navigate between breakpoints. The debugger has paused on the first line of code interpreted, but hit the resume icon to continue. You should then move onto the next breakpoint – the line in your controller’s index() action. From here you are right inside your Cake application’s execution, and can debug it just like any other PHP script using the full power of Zend Studio for Eclipse and Eclipse PDT.