Lesson Weekend

Starting Apache Server

Alright! Let's fire up our server and check out our application. This time instead of navigating to our web folder in the terminal and using the command php -S localhost:8000, let's use MAMP to view our project. Open preferences>ports on your MAMP application. The Apache Port number should be 8888. Now go to preferences>web server and click on the little file folder next to document root. We want to click on the web folder of our project and then hit select in the bottom right corner of the popup. Now we can hit ok at the bottom of the preferences modal and make sure our server is started. With that, we should be able to navigate to localhost:8888 and view our to_do list.

Potential error: If your project loads to your landing page but you're unable to use any of you other routes you need to add the following configuration file into your web folder in order that Apache and Silex know how to play nicely together.

web/.htaccess
<IfModule mod_rewrite.c>
  Options +FollowSymLinks -MultiViews
  # Turn mod_rewrite on
  RewriteEngine On
  RewriteBase /

  ## If the request is for a valid directory
  RewriteCond %{REQUEST_FILENAME} -d [OR]
  ## If the request is for a valid file
  RewriteCond %{REQUEST_FILENAME} -f [OR]
  ## If the request is for a valid link
  RewriteCond %{REQUEST_FILENAME} -l
  ## don't do anything
  RewriteRule ^ - [L]

  # if requested files exists in /web then forward it there
  RewriteCond %{DOCUMENT_ROOT}/web/$1 -f
  RewriteRule ^(.+?)/?$ /web/$1 [L]

  # if requested files doesn't exist in /web then forward it to index.php
  RewriteCond %{DOCUMENT_ROOT}/web/$1 !-f
  RewriteRule ^(.+?)/?$ ./index.php [L]
</IfModule>

For more reading on the problem this addresses, check out this article on mod_rewrite. The terminal commands in the article are written for Ubuntu, but the descriptions apply for our case as well.

Debugging with Apache

It won't take long for you to notice that Apache does not print the PHP error messages we've been used to seeing so far straight to the browser like our local PHP server did. Don't worry! They still exist. Let's find them... get ready to feel like a real developer!

Instead of printing the errors to your DOM apache actually keeps logs of all errors in a file called php_error.log. This file is found in the MAMP/logs/ folder in your applications. To get to it, in your terminal, enter the following commands:

$ cd ~/../../Applications/MAMP/logs/
$ atom php_error.log

The path to the logs/ directory may be different on personal computers, but this file will give you access to the errors you used to have in your browser. Each is labelled with a time stamp, and the most recent are at the bottom of the list.