We recently made the decision to move our codebase to Laravel. To accomplish this we had to make some difficult decisions about where our code would live and how we could accompolish this and how we're going to work toward modernizing our old code.
For reference, our current side is a spaghetti mess of procedural PHP that makes every new developer that we bring on board here cringe. For example, we have a crucial component of our codebase that uses a for loop that is well over 1,000 lines long and is called constantly. Last summer we had an intern that introduced a sub loop in the middle of this, not realizing he was already within a loop referencing $i. As you can imagine, the resulting 'Loopgate' created millions of error logs in the database and was a bit of a mess to figure out.
What we're doing is using a relatively straightfoward series of .htaccess directives while also adding some environment variables to our server to make sure everything plays nicely together.
For me, however, this completely broke my dev environment.
This post is about what I did to fix it. I'm hopeful that someone else might find it useful as well should they be searching to solve similar problems.
For reference, I am running a MacBook Pro (retina) 13" late 2013 on OS X 10.9.2 Mavericks and my environment is MAMP Pro v2.2 (Not 3).
The first and most frustrating of problems to overcome was adding an environment variable to my vhost for MAMP. I dug around my system files trying to find where my virtual hosts were loaded and eventually found them. I added my include information:
php_value include_path ".:/Applications/MAMP/bin/php/php5.5.3/lib/php:/Users/fskirschbaum/Sites/LARAVEL_INSTALL/dev/public/ex/"
(Quick aside, the reason for that is we had all of our old code living in that ex/ folder and needed to do a little magic with prepending a script to make everything continue to work.)
Well, upon restarting MAMP my clever addition was deleted spectacularly from that file. Of course, it took a bit of troubleshooting before I realized it.
Turns out that this is all handled simply from the MAMP Pro window:
You can see here that I was simply making things MUCH more difficult than necessary.
Access MAMP's PHP
First thing I was going to do, on advice of a fellow dev, was to get Composer working in the hopes that the beauty and majesty that is composer would be able to fix things through automagic. Sadly, Composer, while a great dependency manager, is not as easy to get up and running in Mac OS X as, say, Node, or just about any other package.
They do supposedly have Homebrew installation for global use: (from their instructions):
Globally (on OSX via homebrew)
Composer is part of the homebrew-php project.
sh brew update brew tap josegonzalez/homebrew-php brew tap homebrew/versions brew install php55-intl brew install josegonzalez/php/composer
That's it. There's nothing more on their site about getting this to work, and let me tell you a few things. First, this method resulted in so many errors out of the gate that I couldn't even scroll back through them all, however, I can tell you that the gist if it was I didn't have homebrew-php and getting that wasn't going to work because I was exceeding Github's API limit and needed to get an API key.
After doing a quick search I found a great Gist that had clear instructions on getting Composer installed Globally using MAMP's PHP.
TL;DR: for me was:
- Restart terminal to reload profile.
curl -sS https://getcomposer.org/installer | phpmamp
sudo mv composer.phar /usr/local/bin/composer
This got Composer installed for me using MAMP's PHP binary, however, there were other problems that I didn't that resulted in some errors when trying to use Composer. I was getting errors regarding Mcrypt and others while trying to issue Composer commands. It occured to me that the reason was that I had installed Composer with MAMP's PHP but that I might be having issues with my Global PHP install.
So, I changed my
PATH in my
Now I can see that I am using the PHP I want to use:
macbookpro:~ fskirschbaum$ which php /Applications/MAMP/bin/php/php5.5.3/bin/php
Finally I was really to get things cooking, I thought. As per the previously mentioned advice I issued forth from my fingers the command that would fix everything:
The results were underwhelming, to the say the least. It said it was generating files and returned me instantly to my prompt. Fired up a browser and... sad-trombone.
Then I tried:
That also failed supremely. I noticed, however, that I was getting strange permission issues.
Well, permission issues can be an interesting bag.
This turned out to be a bit of an issue with the way that MAMP deals with your file system and permissions. You see, with Laravel installed I am pointing MAMP at my public/ folder for my webroot, which makes perfect sense. However, permissions to read and write from the application folders above the webroot were causing me some measure of headache.
What I did for my development environment:
chmod a+wx -R dev/
You probably wouldn't want to do that for the sake of security because that essentially says that anyone can write and execute those folders from anywhere any time ever. That's not good. Were I not completely certain that my own set-up was secured in other areas, I would never do this. That, however, is up to you.
If you don't want to do that, you may want to consider how you handle your permissions and take appropriate actions. A pretty good post on Apache permissions by Paul Mason can be found here. Essentially he is describing giving your user the ownership necessary of all the files. There are other pitfalls here that I wanted to avoid given our particular development environment. Your mileage may vary.
Now, quite simply, I'm ready to rock. My Composer troubles are over. I'm no longer seeing the permission errors, nor am I having any trouble with mcrypt or any other modules. Everything seems to be working exactly as I need and I'm up and running and ready to begin painstakingly modernizing our code piece by piece.
Speaking of modernizing, The Great Mr. Paul M. Jones (who has become a new personal hero after I saw him speak at MidwestPHP 2014) has a phenomenal book available called Modernizing Legacy Applications in PHP that is an absolute must if you need to do this as well.