Using Laravel Valet for Custom URL Generation and Application Launch

February 01, 2023
Written by
Kenneth Ekandem
Contributor
Opinions expressed by Twilio contributors are their own
Reviewed by

Using Laravel Valet for Custom URL Generation and Application Launch

Laravel Valet is a super lightweight development environment for macOS. It allows Laravel developers to host and run applications locally in the local development environment, yet be referred to with the "*.test" domain. This eliminates the need to manage the host files (/etc/hosts). What's more, by using ngrok, local applications can be made publicly available.

Use cases for Valet

Launch applications locally: with Valet, PHP, and by extension, Laravel applications can be launched on the local development machine without requiring MAMP or Vagrant. This cuts the need for a time-intensive setup and configuration. In short, it does this by serving PHP applications with NGINX, and uses DnsMasq to proxy all requests on the "*.test" domain to the sites served by NGINX.

Generate custom URLs: By default, PHP applications are expected to run on http://localhost with, optionally, a port other than 80. Valet scaffolds that process and allows for a local domain name to be created based on the project's directory name. By doing this, you can use a URL such as "http://xzy.test" in your browser and run your application effortlessly.

Secure links: Securing sites in Valet means making them unique to the specified directory. Valet then creates a unique SSL certificate and binds it to the directory. The SSL certificate keeps user data secure, verifies ownership, and prevents hacks.

Share local environments: With Valet, locally developed applications can be shared using generated URLs for public access and interaction. This enables collaboration on projects, and simplifies reviews before applications are pushed to a production server.

Prerequisites

  1. Prior knowledge of PHP
  2. PHP
  3. Your preferred text editor or IDE. I recommend using Visual Studio Code.
  4. Homebrew
  5. Composer installed globally
  6. ngrok, plus a free ngrok account

Install Valet

To install (and start) Valet, open the terminal on your Mac and run the below commands

brew update
composer global require laravel/valet
valet install
composer global update

Once the installation is completed, check that Valet is installed using the command below.

valet --version

If you see output similar to the following in your terminal, then Valet has been successfully installed on your Mac.

Laravel Valet 3.3.0

Serve a new site using Valet

Now, it's time to go through how to use Valet to serve a PHP application. This will show the simplicity of Valet, regardless of how bulky, complex, or simple the application you are building locally is.

Using your preferred text editor or IDE, create a new folder with the name valet_article. Then, change into that directory and create a new file named index.php. Inside index.php, add the following code:

<?php
echo "Hello world";

When that is done, link the directory to a *.text domain, by running the command below.

valet link  

You should then see output, similar to the example below, printed to the terminal.

// A [valet_article] symbolic link has been created in [/Users/MAC_NAME/.config/valet/Sites/valet_article].

Now that the link has been created and the domain generated, open it in your Mac's default browser. To do that, run the command below.

valet open

Below is an example from my Mac.

Site being served locally by Laravel Valet

Note the URL http://valet_article.test is opened. This matches the name of the folder that you created and linked to. Also, if you were to open your browser's Developer Tools, and then open the Network tab, as shown in the Chrome screenshot below. You should see that Valet is using 127.0.0.1 as the remote address and generating a random port for the application.

Chrome Developer Tools

Secure the site

The next thing to do is to secure the application to prevent hacks. To do that, run the command below. Enter your password or use Touch ID when prompted.

valet secure

The command will create SSL/TLS certificates for the generated URL, load them into and restart NGINX (which was installed by Valet). Please be a little patient while this happens

Once an application has been secured, if you need to, you can make the application directory insecure again, by using the following command. This will remove the security certificate generated and embedded in the project directory.

valet insecure valet_article

Share the site publicly

Valet uses ngrok to make local applications publicly accessible on the internet.

valet share

The above code will start an ngrok session, launch the PHP application, and then publish the URL with its port to a random ngrok address. The image below is an example of what you should see in the terminal.

ngrok session spawned by Laravel Valet

The link in the Forwarding keys are the ngrok auto-generated URLs that can be opened in the browser, which you can see an example of below.

View the site through the public ngrok link.

Note that it still points to http://valet_article.test:60 as the origin URL.

That's how to use Laravel Valet to share a PHP site

In this article, we discussed Laravel Valet and its use primary case. We also went through the process of installing and using it on macOS to act as a development environment for hosting PHP applications. Later, we discussed how to run, secure, and share our project publicly for other users for reviewing or testing.

Kenneth Ekandem is a full-stack developer from Nigeria currently in the blockchain space, but interested in learning everything computer science has to offer. He'd love to go to space one day and own his own vlogging channel to teach the next generation of programmers.