Coda 2 comes with PHP previewing right out of the proverbial box. Give it a try, create a new PHP doc and paste this code into it:
<?php echo "Hello World!" ?>
Then press the preview button and you should see the properly executed output “Hello World!”. This won’t work if you try dragging the PHP file to a web browser. Coda 2 is doing some magic behind the scenes.
The problem is PHP “includes” don’t work without some configuration. Instead you get a nasty error code telling you the file does not exist. I solved this with a few steps:
- Download and run MAMP. Make sure in the preferences that the Apache port is set to 80 (I don’t remember if this is default or not).
- Create a new site in Coda 2 and give it a nickname.
- The only really important fields for local development are Local URL and Local Root. Set the Local Root first by clicking the “Set” button and navigating to the “htdocs” folder that MAMP should have created when you installed it. The path should be Applications/MAMP/htdocs. Create a new folder within the htdocs folder and choose it as the Local Root.
- Now set the Local URL as http://localhost/the_name_of_the_folder.
- You can fill out the other two fields, Root URL (the URL it uses to make an icon for your site) and Remote Root (for uploading to a remote server when you’re ready to go live), if you want but you can leave them blank for local development purposes.
- Now save the site and you should be good to go. If includes still don’t work, try restarting Coda 2 and MAMP.
To test, make a PHP document in your root folder and paste this HTML into it:
Save it as include.php. Now make another PHP document in your root folder and paste this into it:
<?php include('include.php') ?>
Save it as index.php then preview it and you should see in big bold letters “includes work!”
I would still like to know if it is possible to do this without having to save my site in htdocs, but rather, point MAMP to my websites folder in my home folder.