CGX Blog We'll see where this takes us.

1Apr/111

Local Web Development Using Parallels and PHP Includes

Compatibility testing is critical for web development. I work on a Mac so, in the past, I had to have a second computer running windows in order to complete the dreaded IE compatibility test. Eventually, I upgraded to BootCamp so I needed only one computer. This, however, is still a pain because you need to completely reboot into either mac or windows.

Enter Parallels

Parallels is a great way to quickly test Windows-browser compatibility without rebooting at all! I was able to use my existing BootCamp installation so there was no need to reinstall windows, re-download the hundreds of update files, etc.

Connecting to your Local Web Server from Guest OS

The (somewhat) tricky part is ensuring you can connect to your local web development server from your Windows guest OS in Parallels. Here are the steps I took, and it's working well so far:

  1. Install Bonjour for Windows
  2. Enable file sharing on your mac, set your computer name however you like
  3. Voila - pointing to your mac's name in a windows browser should work. I named my computer "mymac" so when in Parallels I can point my browser to "mymac.local" and it points to my mac's localhost.

That should do it. If that does not work then I recommend a google search for "local web development parallels" and see some of the alternative methods that exist.

File Includes and the Relative Path

What if I include other files such as CSS, Javascript, or templates in PHP? Unfortunately, it's not always as easy as writing a relative path. If you know for a fact that the file you are referencing is one directory above the current file or in the same directory then you can use "../fileName.php" or "fileName.php" respectively. If the file you are referencing is in the root and/or you will be referencing it at various points in your site and always want to start from the root, you cannot use "/fileName.php" because your http host is different on your native vs guest OS (on your mac it may be "http://localhost:8888/siteDirectory/" whereas on your guest OS will be something like "compname.local/siteDirectory/").

To get around this in PHP you can simply define a $root variable that resolves the current HTTP host so your references all point to the correct location.

$root = "http://" . $_SERVER["HTTP_HOST"] . "/siteDirectory";

With this defined you can call all of your includes like you see below:

require_once ("$root/includes/functions.php");
<link media="screen" rel="stylesheet" href="<?php echo $root; ?>/css/core.css" />

Conclusion

This has worked for me thus far. If you have feedback on this with respect to the transition to a live web server I'd love to hear it. In the past I tended to define my $root as $_SERVER['DOCUMENT_ROOT'], which works on the web but not on my computer.

Comments (1) Trackbacks (0)
  1. The step by step illustration is very informative and valuable.


Leave a comment


*

No trackbacks yet.