Laravel Valet

Laravel Valet is a new developer environment for the Mac that configures your Mac to always run PHP’s built-in web server in the background when your machine starts.

It automatically maps any .dev domain to your folder structure using DnsMasq. For example, if your project folder is “new-blog-879” then hitting “new-blog-879.dev” in your browser will load this folder.

Valet helps reduce the barrier to entry for running a local PHP web server and runs with a small footprint. Perfect for those old machines with not much RAM.

It’s not meant to be a replacement for a virtual machine like Homestead, Vagrant, or the likes. Instead, it’s opinionated and perfect for spinning up those new ideas quickly.

My computer setup is a work desktop machine that sits in my office and runs Homestead and I use it from 8 AM to 5 PM every weekday. Then at night I play around with side projects or look at new packages on an old Macbook. This is where speed and the time savings that Valet offers is huge. Instead of spending ten minutes setting up a new site through Homestead I can instantly start working. Of course, when I say ten minutes it’s always longer because I easily get sidetracked. Now, however, I have no excuse, the server is ready and waiting for me to start working on my next big idea.

Let’s take a look at how Valet works by spinning up a sample Laravel app and going through the process.

Installation

Installation is simple and it does require Homebrew. You’ll need to install that and then install PHP 7:

brew install php70

Note: You may get a warning such as “No similarly named formulae found.” If you hit that install:

brew install homebrew/php/php70

Next install Valet from composer:

composer global require laravel/valet

Finally, run the installer:

valet install

The install only has to run once and it sets Valet’s daemon to launch when your system starts.

Your first Valet site

Now that Valet is installed, it’s time to test out our first site. I created a new folder to hold all my sites at “~/Sites”. cd into this directory:

cd ~/Sites

Next run the Valet park command:

valet park

The park command tells Valet this is the location where all your sites are and it then maps the URL to folders in this directory. To show you an example create a new Laravel app:

laravel new todoapp

Once it finishes installing visit todoapp.dev in the browser and you should be greeted with the Laravel 5 placeholder page.

Now any new sites added are automatically picked up by Valet and you change the URL in your browser.

Not everyone keeps all their projects in a single folder and you can “park” multiple folders. Just “cd” to another one and run park again to add additional.

Sharing Sites with Laravel Valet

There are times when you are working locally that you would like to share what you see with others. If you are not in the same location this can be troublesome.

Valet ships with a tool called ngrok so you can share with anyone in the world. Here is all that is required to share your site:

cd ~/Sites/todoapp
valet share

After running the command it automatically adds a URL to your clipboard to share with others and shows you an overview of Tunnel Status and HTTP requests:

By sharing sites this gives you an added bonus of being able to use the share URL to test webhooks from services like Stripe, Mailgun, etc. Perfect for local development when testing webhooks are usually tricky.

Sites outside of your parked directory

If you have sites in other places on your file system you can still utilize Valet but utilizing it’s “link” command.

cd ~/Ideas/zonda
valet link zonda

Now you can hit zonda.dev in the browser and this site will load.

If you ever want to see a list of linked sites Valet offers the following command:

valet links

Then if you ever want to remove one:

valet unlink zonda

Securing a Laravel Valet site with SSL

Valet uses plain HTTP by default and with the release of 1.1.9 you can now easily serve sites over HTTPS by running:

valet secure zonda

Then to revert back to HTTP:

valet unsecure zonda

This is all managed through Caddy Server.

Laravel Valet with MySQL or MariaDB

Of course, almost every project you build is going to need to utilize a database. The database setup is outside the scope of Valet but it’s simple to get started. The documentation recommends MariaDB and currently it is a drop in replacement for MySQL. So let’s set that up and just note that MySQL would be installed basically the same way.

Using homebrew run the following:

brew install mariadb

Then start the service:

mysql.server start

Finally, connect through Sequal Pro or your tool of choice:

The settings are as follows:

  • Host: 127.0.0.1
  • Username: root
  • Password: (leave empty)
  • Port: 3306

Laravel Valet Drivers

Drivers are a unique feature that let you run any type of PHP app. This is needed because different apps require different URL rewriting rules. Valet comes preinstalled with drivers for Laravel, Lumen, Statamic, WordPress, and CraftCMS.

If you are wanting to run a different app then you will need to create a new driver. The documentation is going to be the place to look for deeper instructions on this as each app is unique in their requirements.

Learning more about Laravel Valet

As I said earlier I’m very excited about Laravel Valet. It solves a legitimate pain point for a lot of developers and is simple to use. With the release of Valet, I’ve been able to ditch Mamp and get everything running quickly.

No more worrying about vhosts, fiddling with YAML files, or anything else. Stop the configuration madness and start developing as soon as the inspiration hits.