Code Smart: Valet

Laravel is a piece of software that exists within the Laravel ecosystem to allow you to run web applications using the stack that is already installed on your development machine. For example, you might already have PHP installed locally, or a copy of MariaDB to use as a database. Homestead would run a VM containing many types of software that can be used with Laravel, but with Valet; you install only what you need.

Personally, I would recommend using Homestead, as it will allow you to version your running environment alongside your code. This is an incredibly useful advantage, especially when working within a team. However, it is understood that Homestead is more complicated than Valet.

If you're not comfortable with Homestead, the continue reading to learn how to be up and running with Valet in no time at all.

Installation

Since Valet uses the software on your computer to run its stack, then you'll first need to install at least PHP. Remember, Valet will only work on a Mac, so if you're using a Windows or Linux machine, then I'd just skip to the next chapter at this point.

On a Mac, PHP can be installed using the 'Homebrew' software. It would be best to check the Homebrew website for instructions on how to install the software. Once it's installed, you can install PHP using the following command:

brew install homebrew/php/php70

Once you've got your copy of PHP installed, we'll continue to install the Valet binary. This can be achieved using Composer. If you'd like to find out how to install Composer, then please check the previous chapter. It would be pointless to repeat this information, wouldn't it?! We are programmers after-all. We hate repeating ourselves!

We'll install Valet globally by running the following command:

composer global require laravel/valet

You may need to add Composer's binary path to your system PATH variable. You'll be able to do that for the current session using the following command.

export PATH="$PATH:$HOME/.composer/vendor/bin"

Next, install the Valet service with the following command.

$ valet install

WARNING: Improper use of the sudo command could lead to data loss
or the deletion of important system files. Please double-check your
typing when using sudo. Type "man sudo" for more information.

To proceed, enter your password, or type Ctrl-C to abort.

Password:
[dnsmasq] is not installed, installing it now via Brew... 

Valet installed successfully!

Great! Valet is now installed. Let's start using it, shall we?

Adding Sites

Valet is fully equipped to serve multiple applications, so first we'll need to 'park' our application directory. Let's say that we're storing our projects in ~/projects. We'll navigate to the projects folder and create a new Laravel project using Composer. We'll call our project 'blog' for example purposes.

$ composer create-project laravel/laravel blog

Next, we'll navigate to and park our applications directory.

$ cd ~/projects
$ valet park
This directory has been added to Valet's paths.

That's it; you're done! Honestly!

You see, any project within your parked directory will be served under the hostname projectname.dev. For example, the blog we created above will be accessible at blog.dev. If you visit the URL, you'll see the Laravel welcome page.

Don't forget to install some database software if you'd like to store information later. Do you see why I prefer homestead yet? Well, each to their own I suppose!

Valet Commands

If you'd like to start, stop or restart the valet daemon that is serving your applications, then the following commands are available.

$ valet stop
$ valet start
$ valet restart

Sharing

Valet offers a useful shortcut to be able to share your applications with others. Please note that this is a tool for collaboration and that you shouldn't use this to serve your applications for the long-term! Simply use the share command as follows:

$ cd ~/projects/blog
$ valet share

On the screen that appears, you'll find a 'Forwarding' URL that can be used to share your applications with others. You'll also see the requests as they pour into your application.

T> If you'd like to use this sharing feature with Homestead, or any other stack, then take a look at the 'ngrok' website.

We've now got everything we need to start working with Laravel. It looks like it's time to move on to something new. In the next chapter, we'll take a look at the lifecycle of a modern PHP application. Do your best superhero pose and flip that page.

My books are available online for free to encourage learning. However, if you'd like for me to keep writing, then please consider buying a digital copy over at Leanpub.com.

It's available in PDF, ePub, and Kindle format, and contains a bunch of extras that you won't find on the site. I have a full-time job, and I write my books in my spare time. Please consider buying a copy so that I can continue to write new and exciting books!