How to set up your Mac for local PHP Development

As a developer I spend countless hours on computer. Over time I accumulate a ton of cruft. Everything from old forgotten files, unused apps, and worst of all hidden junk that has been installed by following some random tutorial.

This past weekend I decided it was finally time to wipe my Macbook’s hard drive and start fresh. I have used it daily for several years now and still had artifacts from when I used Mamp. Since then Vagrant has turned to my local server of choice and one of the reasons is how clean you can keep your machine by utilizing it.

After finishing the new Mac OS X install it felt like a new beginning. So clean, so minimal. I’ve missed that.

This go around I wanted to keep it as minimal as possible and only install things I know I need and use. This tutorial covers how I set up my Mac for local PHP Development.

ZSH Shell

ZSH is a bash alternative and can be easily installed using an open source project named “Oh My ZSH”. I prefer this route because it has numerous plugins and themes that come preinstalled. So customizing is easy.

Their site will give you instructions for installing and it’s all done automatically via a curl or wget call.

Since mine was a fresh machine this took a while to install because it also handles any requirements. Git, Xcode, etc. Just follow the on-screen instructions until it finishes.

Homebrew and PHP

The next step is to get PHP running on the local machine. It is needed for local PHP so later running a linter, composer, etc is all available to my apps.

Homebrew, labeled as the missing package manager for OS X, allows you to easily install items that are not standard from Apple.

You can install Homebrew by running the following terminal command:

ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"

Just as before, follow the on-screen steps until it completes.

Once it finishes, move on to installing PHP:

brew tap homebrew/dupes
brew tap homebrew/versions
brew tap homebrew/homebrew-php
brew install php56 --with-homebrew-curl --without-bz2 --without-apache --without-fpm --without-ldap --without-mysql
brew install mcrypt php56-mcrypt

These commands will setup a very basic php 5.6 without a lot of the items that typically come installed like Apache, fpm, ldap, MySQL, etc.

Composer

Composer is the PHP package manager. Almost all frameworks and packages are now using it to handle dependencies so it’s good to have it installed locally.

We can easily install it by running the following in terminal:

curl -sS https://getcomposer.org/installer | php

After it completes move the composer.phar executable into your terminals $PATH:

sudo mv composer.phar /usr/local/bin/composer

After this you may have to restart terminal but you should be able to run composer and get a list of available commands.

Vagrant

Vagrant is a tool for building a local virtual development environment. It allows you to run another operating system within your current one. The advantages to using a virtual machine is that everything is self contained.

If you make a mistake, just destroy the machine.

Another use case is you can mirror your production server which will allow you to confidently ensure you are developing to the same requirements.

To give you an example, a few years ago PHP changed it’s array syntax and any old array code would continue to work. However, the new style would throw out a nasty error if the PHP version running didn’t support it. By having your local machine mirror production you can instantly see the error of your ways.

Installing Vagrant is really simple as they offer binaries for Mac OS X, Windows, and Linux. Just head over to their site and download the one that’s right for you.

After it installs it’s time to setup a new Vagrant box and I choose to use Homestead by Laravel.

Homestead

Homestead is created by Laravel but it’s not specific to the framework. You can run any PHP app on it.

What I enjoy about Homestead is that it allows you to create a virtual machine in the style you like. Most Vagrant tutorials recommend you have a machine for each of your apps but I came from using Mamp and I like having one master VM with all my apps. That way I can quickly switch between anything at any time.

To setup Homestead run the following terminal command:

vagrant box add laravel/homestead

This downloads a machine image and it’s pretty hefty, so it will take a few minutes based on your internet speed.

While it’s downloading I open a new terminal tab and create a new Code directory to hold all my apps:

mkdir ~/Code

Next cd into Code and clone the Homestead git repo:

git clone https://github.com/laravel/homestead Homestead

Now, cd into Homestead and run its installer:

bash init.sh

This init.sh file does is create a new directory at ~/.homestead with a yaml file for app settings and two extra files for basic settings. These are after.sh and aliases.

Now open ~/.homestead/Homestead.yaml in your favorite editor. The part we are concerned about is the folders, sites, and databases.

Folders are where your code is located on your machine. By default it is ~/Code just as we created earlier.

Sites are your local apps. For example if I wanted my next app to be cakesandpies then I’d use:

sites:
    - map: cakesandpies.app
      to: /home/vagrant/Code/cakesandpies/public

What this basically says is, map the cakesandpies.app url to the directory specified. Homestead uses this to create a vhost on the virtual machine to match them up.

Finally in the databases section you can list out any you want automatically created.

databases:
    - homestead
    - cakesandpies

Now save this file and it’s time to get it all running. cd into ~/Code/Homestead and run vagrant up. A short while later it should say it’s up and running.

Before we can access our app from the browser we will need to add a local domain to our hosts file. Open /etc/hosts in your favorite editor and add a new line at the bottom:

##
# Host Database
#
# localhost is used to configure the loopback interface
# when the system is booting.  Do not change this entry.
##
127.0.0.1   localhost
255.255.255.255 broadcasthost
::1             localhost 
127.0.0.1   cakesandpies.app

Save it and now create a new file at ~/Code/cakesandpies/public/index.php. Just put something like:

<?php
echo 'Cakes and Pies!';

Now hit the url in your browser. Be sure and append the port of 8000 which is Homestead’s default. http://cakesandpies.app:8000

If all went as planned you should see “Cakes and Pies!”.

Conclusion

These steps will hopefully allow you to get a fresh machine up and running for PHP development pretty easily. If you have any questions feel free to comment below or hit me up on Twitter. A huge thank you to Taylor Otwell and his Gist for making this easy.

Laravel and Stripe

Over the past few years, I’ve implemented Laravel and Stripe on multiple occasions. Everything from subscriptions to one-off purchases. When I started, Laravel Cashier wasn’t invented yet and it was a totally different beast, but now with Cashier it takes a lot of the pain away by having a simple API.

But with selling products and subscriptions there are many other aspects you need to think about and it’s easy to get intimidated thinking about all the features you need. Or worse, where to even start?

I wanted to share my knowledge on the subject and teamed up with an experienced author, W. Jason Gilmore, to create a new book on the subject, Easy E-Commerce using Laravel and Stripe. Jason has authored numerous books and has also built a 10,000+ product online store and a SAAS for the interior design and architectural industries.

We wanted to create a fun hands-on book taking you from the start of a project all the way through implementing product sales, digital downloads, and subscriptions.

The book is written around a fictional lawn care company that has hired you. But Mr. McDew, the owner of the company, is a stickler and wants to be sure you know what you are doing. So after each project phase he drills you with questions about the implementation, and if you answer correctly you get to move on to the next phase.

No web development book would be complete without sample code and we include many code samples, plus a complete companion project. This allows you to use it not only as a learning resource but you can run the app locally to test and play around with.

Some of the highlights include:

  • You will receive all of the source code to a real-world online store
  • Comprehensive, step-by-step instructions showing you how to integrate Stripe into your Laravel application using Cashier.
  • Learn how to integrate Stripe in a fun, entertaining, and unintimidating fashion by following along with the creation of a real-world project for a fictional company.
  • You’ll learn about many of the concepts central to building an online store, such as how to build a product management interface, and a one-time URL generator for downloading electronic products.

We also cover other Stripe features such as the “buy now” modal window, validating credit card forms, adding coupons and discounts, swapping subscriptions, and even implementing custom Stripe web hooks for sending emails.

Save yourself time and learn how to implement Laravel and Stripe today.

Slack vs Hipchat – It’s not the design

I came across two posts discussing the design aspect of Slack and it’s $2.8 Billion Dollar Secret Sauce with a follow up from a designer working on HipChat, Slack’s design is not secret sauce.

Both of these got me thinking about the differences in the apps and for me they are both wrong. Both designs are good enough for my everyday communication. The only reason Slack is winning is because of the multiple account logins. At work, we used Hipchat, and it was a fine product that got the job done. Where it started lacking is when I would get invited to another companies account. The only way to use both was to have the browser open for one and the app for the primary. It was horrible.

That single feature alone made me champion the move to Slack. Of course, the ability to create public channels so open source projects, and teams can move off the dreadful IRC is another wonderful feature.

Lessons learned from running a weekly newsletter

Yesterday marks the one-year anniversary of my weekly Laravel News newsletter. I managed to send a new issue almost every week and only missed two because of vacations. I had no idea what I was doing in the beginning, and still every week I’m learning something new about the science behind newsletters. In this post, I want to share some of the behind scenes on running the newsletter, a few stats, mistakes I made, and lessons learned during this first year. Continue reading Lessons learned from running a weekly newsletter

Questions to ask an employer during an interview

When interviewing for a new job it’s easy to fall in a trap of missing some very important questions about the company. The whole interview process is nerve-racking and there is so much focus on you that it’s easy to forget to find out about the internal workings of the company.

The InterviewThis project aims to help you find out the most important questions a developer should be asking before taking a new job.

Some of the topics include:

  • Development Process
  • Codebase / Architecture
  • Monitoring / On-call duty
  • Company Culture
  • PHP Environment – Framework/Composer/Style
  • Remote Life

By having a list of questions like this you can go into the interview fully prepared.

If you are looking for a new job be sure and check out Larajobs as it’s a great resource for finding companies looking for developers.

Slow Motion Golf Swing

I haven’t played golf in year but had the luxury of playing with some friends  on Monday. One of the guys took a slo-mo video of my swing and sent it to me. The way I pictured myself swinging, versus how I actually swing is completely different. I do have a injured right elbow that can not bend which makes golf even more awkward.

Maybe if I start working on it now I’ll be ready for the senior PGA tour?

Gamification of Todos

Ever since a man came down the mountain carrying the first todo list, we have been in search of finding the perfect system for keeping up with our todos. In modern times we have calendars, pencil and paper systems, and more digital todo apps than stars.

I think I’ve tried them all. From the minimal with clear, to wunderlist, to the super advanced of Omnifocus. Nothing is perfect.

A few weeks back I decided to start the search again. At one point, I had four different apps installed and duplicated all my tasks between them. That got old really fast but it helped me whittle down the list to my final choice of Todoist.

todoist-natural-language

I use my todo’s much different than most. I use for it everything from keeping a read later list, to email follow ups, and then typical work/personal reminders. But one feature I really wanted was the ability to set due dates with the time on tasks. The natural language of entering dates makes this really nice.

Todoist Karma
Todoist Karma

The tipping point for me was the gamification aspect of Todoist. By getting points for completing tasks it subconsciously forces me to create tasks that are actionable and can be accomplished.

Git Style Guide

Git Style Guide is a GitHub project aimed at helping you improve your Git practices. Or if you are like me it shows that everything you’ve been doing is wrong.

A real git log
A real git log

I’m probably showing my age here, but I remember using CVS, Concurrent Versions System, and how horrible it was. Every time I attempted to branch and merge I’d break the whole system. Now with Git I feel like you can’t really mess it up.

Have you seen a public repo that you think has a great Git log and branching pattern?

How To: Validate an array of form fields with Laravel

If you’ve used Laravel’s form validation for any length of time, then you know it’s a powerful system. It makes the tedious task of validation very simple while still keeping the door open for complex rules.

In this tutorial, I want to show you a simple and easy way of validating forms that contain dynamic fields. A common use case for these types of forms is when you would like to allow a user to add more fields to a form.

Continue reading How To: Validate an array of form fields with Laravel