Blog

05
Jan

Debugging Magento with Zend Studio 8 on Mac OS X

One of my resolutions this year is to move to a robust IDE for Magento development. There are a lot of opinions out there on which combination of tools is the best, but I have decided to try out the monster that is Zend Studio 8. Previously, I have been using a combination of Komodo Edit and navigating the labyrinth of Magento files by hand. The idea of a debugger and a quick look up of the Magento classes can only improve my workflow.

Laying the groundwork

First, a little about my setup. I am using a Mac laptop with Snow Leopard installed. I have a local Apache web server setup along with MySql and PHP5 that I have previously setup using MacPorts. I use virtual servers to test local versions of websites I’m working on. With this stack, Apache config files are located under /opt/local/ directories. The configuration files that needed editing to get Zend Studio and Zend Debugger working are (note that your directory location may be different):

  • /opt/local/apache2/conf/httpd.conf
  • /opt/local/apache2/conf/httpd-vhosts.conf
  • /private/etc/hosts
  • /opt/local/etc/php5/php.ini

Note that these files are protected, so you may need an editor like TextWrangler to unlock them for editing. We’ll get to those edits in a minute. See also: Setting up virtual hosts on Mac OS X

Zend Installations

Install Zend Studio

Download Zend Studio 8 and install. This part is pretty straight-forward. By default, a workspace is created in your user folder under Zend/workspaces/DefaultWorkspace7. This is where new projects will reside.

I created a new virtual server to point to this workspace location called local.zend.

  1. I added a new virtual host to http-vhosts.conf.
    <VirtualHost *:80>  DocumentRoot "/Users/stacey/Zend/workspaces/DefaultWorkspace7"  ServerName local.zend  <Directory "/" >    Options Indexes FollowSymLinks Includes    AllowOverride All    Allow from all  </Directory></VirtualHost>
  2. I added a new entry to my hosts file (/private/etc/hosts)
    127.0.0.1    local.zend
  3. Restart your web server.
  4. Go to Zend Studio. Open Zend Studio > Preferences and go to PHP > PHP Servers. I set the Default PHP Server to my new virtual host (http://local.zend).
  5. Dreamweaver
    Uploaded with Skitch!

Install and Configure Zend Debugger

Next download the Studio Web Debugger from the same page. Choose the 32-bit or 64-bit version, based on your OS (64-bit in my case for Snow Leopard on my MacBook Pro). The debugger Installation Notes are helpful. Here are the steps I followed:

  1. Extract the Zend Debugger package to a temporary folder.
  2. Locate the ZendDebugger.so file in the directory which corresponds to your PHP (5.2.x in my case).
  3. Copy the file to your Web server in a location that is accessible by the Web server. I copied the file to /opt/local/apache2/modules.
  4. To load the Zend Debugger, add the following line to your php.ini file:
    zend_extension=<full_path_to_ZendDebugger.so>
  5. To authorize the Zend Studio to access the debugger, also add the following lines to your php.ini file:
    zend_debugger.allow_hosts=<host_ip_addresses>zend_debugger.expose_remotely=always
  6. Replace <host_ip_addresses> with the IP of the hosts which will be allowed to initiate debug sessions (this should be the IP addresses of the machines on which Zend Studio is installed. Below is a summary of the lines I added to my php.ini file:
    [zend]zend_extension=opt/local/apache2/modules/ZendDebugger.sozend_debugger.allow_hosts=127.0.0.1,10.37.129.2,10.0.1.193,10.211.55.2zend_debugger.expose_remotely=always
  7. Copy the dummy.php file from the extracted Zend Debugger directory to your document root directory.
  8. Restart your web server.

Set up a Magento project in Zend Studio

With those setup steps out of the way, I moved on to creating a new project. These steps let me point to a remote server and import down the project files. With Magento, of course, there are ton of files, so the import takes a while initially. I am not currently running SVN, but another option is to import project files from a SVN repository. Once the files are imported, you can work locally to develop, test, and debug, then upload the modified files back to the remote server when ready. (That’s the idea at least, but I haven’t gotten that far yet).

To setup a project:

  1. In Zend Studio, go to File > Import
  2. Select “Project From Remote Server” and click Next
  3. Create a connection to your remote server and choose the directory to import.
  4. Leave “Use Default Location” checked.
  5. Click Finish. This starts the import process. Go get some coffee and come back in a while after the import is finished.
  6. You’ll also need to setup a local database for the local instance of the store. I took a mysql dump of the production database, imported it into a new local database, updated core_config_data, among other steps. Read some other tips for setting up a test version of your Magento store.
  7. After the import, the project should now appear in your PHP Explorer panel in Zend Studio.
  8. At this point, you can test that your local store is working by trying to navigate to your new virtual host, and the particular project directory. In my case, going to http//local.zend brought up a directory listing showing my projects. Opening http://local.zend/tagottis launched my new Magento install.
  9. Back in Zend Studio, right-click on the project and go to Properties.
  10. For PHP Build Path, I excluded a few folders that are outside of the php functions such as media and skin. I believe this helps the debugger perform faster since it doens’t need to traverse these directories.
  11. Dreamweaver
    Uploaded with Skitch!
  12. I also set the PHP Include Path to the app and lib directories.
  13. Dreamweaver
    Uploaded with Skitch!
  14. Next, I setup a Debug configuration so that I can set breakpoints and use the true power of Zend Studio to dive into the code. Go to Run > Debug Configurations.
  15. Click New under PHP Web Page. I renamed the configuration to reflect my project, Tagotti Shoes Magento. Set the file to the index.php file for your Magento store. Uncheck “Auto Generate” and set the path to your Magento root directory. On the Common tab, check Debug and Run to add the configuration to your favorites. Click Apply.
  16. Dreamweaver
    Uploaded with Skitch!
  17. Now to test things out. I added a breakpoint to one of my module files (line 37 in the screenshot below). This module displays a listing of all product manufacturers on a page of the store. This code should run when I navigate to the “Designers” menu item for my store.
  18. Dreamweaver
    Uploaded with Skitch!
  19. Debugging should now be setup. I click down arrow next to the Debug button (the little bug in the toolbar) and select my project (Tagotti Shoes Magento). I am prompted to open the Debug perspective, which is just a view that provides panels with info relevant to the debugging process and click Yes.
  20. A browser opens up showing my Magento home page along with a lot of other info in the other panels. Cool. Next I click “Designers” to go to my breakpoint. The screen shot below shows my view when I am stopped at the breakpoint. I can look at the debug tab to see the preceeding calls that brought me to this point in the code.
  21. Dreamweaver
    Uploaded with Skitch!
  22. I can also use the Ctrl-click function to navigate to the parent class of my custom class and also view methods available for an object as I type.
  23. Dreamweaver
    Uploaded with Skitch!

Off we go!

That’s all for now, folks. I hope my experiences are helpful to others out there trying to setup Zend Studio for developing and debugging Magento projects. I still have a lot to learn about the tool, and plan to keep posting as I learn more useful tips.

Be Sociable, Share!