Laravel Valet (Mac development environment)


1. Overview

Valet is a minimalist development environment for Mac, without Vagrant, Apache, Nginx or /etc/hosts You can even use a local tunnel to publicly share your site.

In Mac, when you start the machine, Laravel Valet always runs the PHP built-in Web server in the background, and then uses the DnsMasq , Valet delegates all requests to *.dev Domain name and point to the site where the local machine is installed. Such a fast Laravel development environment only needs 7M of memory.

Valet does not want to replace Vagrant or Homestead, but provides another option, which is more flexible, fast, and occupies less memory space.

Valet provides us with the following software and tools:

Of course, you can also extend Valet through customized drivers.

Valet Or Homestead

As you know, Larravel also provides another development environment, Homestead. The difference between Homestead and Valet lies in their target audience and local development methods. Homestead provides a complete Ubuntu virtual machine with automatic Nginx configuration. If you need a complete virtualized Linux development environment or use Windows/Linux operating system, Homestead is undoubtedly the best choice.

Valet only supports Mac and requires PHP and database server to be installed locally. This can be done by using Homebrew Easy command implementation( brew install php70 as well as brew install mariadb )Valet provides a fast local development environment with minimal resource consumption. If you only need PHP/MySQL and do not need a complete virtualized development environment, Valet will be the best choice.

Finally, Valet and Homestead are both good helpers for configuring the local Laravel development environment. Which one to use depends on your personal preference or the needs of the team.

2. Installation

Valet requires Mac operating system and Homebrew Before installation, ensure that no other programs such as Apache or Nginx are bound to the local port 80. The installation steps are as follows:

  • use brew update Install or update Homebrew to the latest version
  • By running brew services list ensure brew services It is valid and can obtain the correct output. If it is invalid, you need to add to
  • Install PHP 7.0 through Homebrew: brew install php70
  • To install Valet through Composer: composer global require laravel/valet (Ensure ~/.composer/vendor/bin In the system path)
  • function valet install Command, this will configure and install Valet and DnsMasq, and then register Valet background random startup.

After installing Valet, try to use the command such as ping foobar.dev Ping any on the terminal *.dev Domain name. If Valet is installed correctly, you will see that 127.0.0.1 Response of:

 PING foobar.dev (127.0.0.1): 56 data bytes 64 bytes from 127.0.0.1: icmp_seq=0 ttl=64 time=0.069 ms 64 bytes from 127.0.0.1: icmp_seq=1 ttl=64 time=0.077 ms 64 bytes from 127.0.0.1: icmp_seq=2 ttl=64 time=0.072 ms 64 bytes from 127.0.0.1: icmp_seq=3 ttl=64 time=0.082 ms

Every time the system starts, the Valet background will start automatically, without having to run manually again valet start or valet install

data base

If you need a database, you can use the brew install mariadb After installing MariaDB, you can use the user name root Connect to the database with a blank password.

3. Service site

After the installation of Valet is completed, the service site can be started. Valet provides two commands for this purpose: park and link

Park command

  • Create a new directory in Mac, such as mkdir ~/Sites , then enter this directory and run valet park This command will take the current directory as the web root directory.
  • Next, create a new Larave site in the new directory: laravel new blog
  • Access in browser http://blog.dev

The screenshot is as follows:

 Use Valet to build the Laravel development environment

This is all we have to do. Now, all Sites All Laravel projects created in the directory can be accessed through http://folder-name.dev Is this convenient to access in the browser?

Link command

link The command can also be used for local Laravel sites. This command is useful when you want to provide a single site in the directory.

  • To use this command, first switch to one of your projects and run valet link app-name , so Valet will ~/.valet/Sites Create a symbolic link in to point to the current working directory.
  • Run complete link After the command, you can use the http://app-name.dev visit.
To view all linked directories, run valet links Command. You can also pass valet unlink app-name To delete the symbolic link.

4. Sharing site

Valet also provides a command to share the local site with others, which can be achieved without any additional tools.

To share the site, switch to the directory where the site is located and run valet share , this will generate a publicly accessible URL and insert it into the clipboard, so that you can copy it directly to the browser address bar. It is that simple.

To stop sharing a site, use Control + C OK.

5. View Log

If you want to display the logs of all sites on the terminal, you can run valet logs Command, which will display a new log on the terminal.

6. Custom Valet Drive

You can also write custom Valet drivers to provide services for PHP applications running on non Valet native support. When Valet is installed, a ~/.valet/Drivers Directory, where there is a SampleValetDriver.php File, which contains an example that demonstrates how to write a custom driver. There are only three ways to write a driver: serves isStaticFile and frontControllerPath

These three methods receive $sitePath $siteName and $uri Value as a parameter, where $sitePath Represents the site directory, such as /Users/Lisa/Sites/my-project $siteName Indicates the main domain name, such as my-project , and $uri Is the input request address, such as/foo/bar.

After writing a custom Valet driver, put it in ~/.valet/Drivers Contents and follow FrameworkValetDriver.php For example, if you are writing a custom valet driver for Wordpress, the corresponding file name is WordPressValetDriver.php

Next, we will discuss and demonstrate the three methods that need to be implemented to customize the Valet driver.

Services method

If the custom driver needs to continue processing input requests, serves Method will return true , otherwise the method returns false Therefore, in this method, the given $sitePath Whether to include items of your service type.

For example, suppose we write WordPressValetDriver , then corresponding serves The method is as follows:

 /** * Determine if the driver serves the request. * * @param  string  $sitePath * @param  string  $siteName * @param  string  $uri * @return void * @translator laravelacademy.org */ public function serves($sitePath, $siteName, $uri) { return is_dir($sitePath.'/ wp-admin'); }

IsStaticFile method

isStaticFile Method will judge whether the input request is a static file, such as a picture or style file. If the file is static, the method will return the full path on the disk. If the input request is not a static file, it will return false

 /** * Determine if the incoming request is for a static file. * * @param  string  $sitePath * @param  string  $siteName * @param  string  $uri * @return string|false */ public function isStaticFile($sitePath, $siteName, $uri) { if (file_exists($staticFilePath = $sitePath.'/public/'.$uri)) { return $staticFilePath; } return false; }
Note: isStaticFile The method can only be used in serves Method return true And the request URI is not / Will be called.

FrontControllerPath method

frontControllerPath Method returns the full path of the front-end controller, usually index.php

 /** * Get the fully resolved path to the application's front controller. * * @param  string  $sitePath * @param  string  $siteName * @param  string  $uri * @return string */ public function frontControllerPath($sitePath, $siteName, $uri) { return $sitePath.'/public/index.php'; }

7. Other Valet Commands

command describe
valet forget Run the command from the "parked" directory to remove the directory from the list of parked directories
valet paths View your "parked" path
valet restart Restart Valet
valet start Start Valet
valet stop Close Valet
valet uninstall Uninstall Valet

give the thumbs-up Cancel Like Collection Cancel Collection

<<Previous: Laravel Homestead

>>Next: Simple task management system