Valet


Installation
  • Upgrade
    Service site
  • Park Command
    • Link Command
  • Build a secure site using TLS
    • Sharing site
    • Customized Valet driver
    • Local driver
  • Other Valet commands
    • Introduction

      Valet is the Laravel development environment for Mac minimalists. Without Vagrant, there is no need to configure the /etc/hosts file. You can even share your site publicly using local tunnels. Yeah, we like it too.

      Laravel Valet sets up your Mac to always run the Nginx service in the background after booting. Valet then uses DnsMasq to proxy all site requests directed to the locally installed computer to domain names ending in *.test.

      In other words, a blazingly fast Laravel development environment only takes up 7MB of memory. Valet is not intended to completely replace Vagrant or Homestead, but to provide another option that is more flexible, convenient, and takes up less memory.

      Valet supports but is not limited to the following:

      • Laravel
      • Lumen
      • Bedrock
      • CakePHP 3
      • Concrete5
      • Contao
      • Craft
      • Drupal
      • Jigsaw
      • Joomla
      • Katana
      • Kirby
      • Magento
      • OctoberCMS
      • Sculpin
      • Slim
      • Statamic
      • Static HTML
      • Symfony
      • WordPress
      • Zend

      Of course, you can also use custom driver to extend your Valet.

      Valet or Homestead

      You should know that Laravel also provides another local development environment, Homestead. The difference between Homestead and Valet is the target audience and approach to local development. Homestead provides a complete Ubuntu virtual machine with automated Nginx configuration. If you want to implement a fully virtualized Linux development environment on Windows/Linux, Homestead is a good choice.

      Valet is Mac only and requires you to install PHP and the database server directly onto your local machine. This can be easily achieved by using Homebrew commands such as brew install php and brew install mysql. Valet provides an extremely fast, minimally resource-intensive local development environment, ideal for developers who only need PHP/MySQL and do not need a virtual development environment.

      Valet and Homestead are both excellent choices for configuring a Laravel development environment. Which one to choose simply comes down to personal preference and the needs of your team.

      Installation

      Valet requires macOS system and Homebrew. Before installation, make sure that no other program (such as Apache or Nginx) is occupying port 80 of the local machine.

      • Use brew update to install or update Homebrew to the latest version.
      • Use Homebrew’s brew install php command to install PHP 7.3.
      • Install Composer.
      • Use Composer’s composer global require laravel/valet command to install Valet. And make sure the ~/.composer/vendor/bin directory is in your system's "PATH".
      • Run the valet install command to configure and install Valet and DnsMasq, and register the Valet background service, which will start automatically when the system is running.

      After installing Valet, you can try to ping any *.test domain name on the terminal using a command similar to ping foobar.test . If Valet is installed correctly, you can see the response from 127.0.0.1 on the terminal.

      Valet will automatically start its process every time the machine starts. So once Valet is initialized, there is no need to run valet start or valet install again.

      Use another domain name

      By default, Valet uses the .test top-level domain name to serve your project. If you want to use another domain name, you can use the valet domain tld-name command.

      For example, if you want to use .app instead of .test, run valet domain app, Valet will automatically change the site's top-level domain name is *.app.

      Database

      If you want to use a database, please run brew install mysql@5.7 in the terminal to install MySQL. Once the installation is complete, you can start MySQL using the brew services start mysql command. You can then connect to the database at 127.0.0.1 using the root username and an empty string password.

      PHP Version

      Valet allows you to switch PHP versions using the valet use php@version command. If the specified version is not installed yet, Valet will install the specified PHP version through Brew:

      valet use php@7.2valet use php

      Upgrade

      You Valet can be updated using the composer global update command in the terminal. After the upgrade, if necessary, it is best to run valet install again so that Valet can upgrade the configuration files.

      Upgrade to Valet 2.0

      Valet 2.0 moves Valet’s underlying web server from Caddy to Nginx. Before upgrading to this version, you should stop and uninstall the existing Caddy process by running the following command:

      valet stop
      valet uninstall

      Next, upgrade Valet depending on how you installed it (usually via Git or Composer). If Valet is installed through Composer, you should use the following command to update to the latest major version:

      composer global require laravel/valet

      If you have updated the source code of Valet, you should execute the install command:

      valet install
      valet restart

      After the upgrade, It may be necessary to reset or relink your site.

      Service Site

      After Valet is installed, you can start setting up the site. Valet provides two commands to serve Laravel sites: park and link.


      ##park Command

        Run
      • mkdir ~/Sites command creates a new directory on Mac. Next, run cd ~/Sites and valet park to use the current working directory as the path for Valet to search for sites.
      • Next, create a new Laravel site in this directory:
      • laravel new blog.
      • Open
      • http://blog.test in the browser.

      that is all. Now, any Laravel project you create in the "parked" directory will automatically be accessed using http://folder-name.test.


      link Command

      Use if you want to serve a single site in a directory instead of the entire directory link command.

        To use this command, first switch to one of your projects in the terminal and run
      • valet link app-name. Valet will create a symbolic link in ~/.valet/Sites pointing to the current directory.
      • After running the
      • link command, you can access the site through http://app-name.test in the browser.

      Run the valet links command to view a list of all directory links. You can also use valet unlink app-name to remove symbolic links.

      {tip} You can use valet link to point multiple (sub)domain names to the same application. To add a subdomain or other domain name to your application, run valet link subdomain.app-name in the application directory.


      Securing the site using TLS

      By default, Valet serves the site over plain HTTP. However, if you want a site to be served over encrypted TLS using HTTP/2, use the secure command. For example, if your site is served on the laravel.test domain via Valet, you should run the following command to protect it:

      valet secure laravel

      To "unprotect" a site and restore it to To serve its traffic over plain HTTP, use the unsecure command. Like the secure command, this command accepts the name of the host you wish to unprotect:

      valet unsecure laravel

      ##Shared site

      Valet even includes commands to share your local site with the world. There is no need to install additional software once Valet is installed.

      To share a site, navigate to your site directory in Terminal and run the

      valet share command. A publicly accessible URL will be inserted into your clipboard and can be pasted directly into your browser. That's all.

      To stop sharing your site, press

      Control C to cancel the process.

      Custom Valet Driver

      You can write your own Valet "driver" to provide services for PHP applications running on other frameworks or CMSs that Valet does not originally support. When you install Valet, a directory ~/.config/valet/Drivers is created containing the SampleValetDriver.php file. This file contains a sample driver implementation that demonstrates how to write a custom driver. Writing a driver only requires you to implement three methods: serves, isStaticFile and frontControllerPath.

      All three methods accept $sitePath, $siteName and $uri values ​​as parameters. $sitePath is the fully qualified path on your machine that serves the site. For example /Users/Lisa/Sites/my-project. $siteName is the "host"/"site name" part of the domain (my-project). $uri is the incoming request URL (/foo/bar).

      Once you have completed your custom Valet driver, place it in the ~/.config/valet/Drivers directory using the FrameworkValetDriver.php naming convention. For example, if you wrote a custom valet driver for WordPress, your file name would be WordPressValetDriver.php.

      Let's look at a sample implementation of each method that a custom Valet driver should implement.

      serves Method

      If your driver should handle the incoming request, the serves method should return true. Otherwise, this method should return false. Therefore, in this method, you should attempt to determine whether the given $sitePath contains an item of the type you are trying to provide.

      For example, let's pretend we're writing a WordPressValetDriver. Our serves method might look like this:

      /**
       * 确定驱动程序是否满足请求。
       *
       * @param  string  $sitePath
       * @param  string  $siteName
       * @param  string  $uri
       * @return bool
       */
       public function serves($sitePath, $siteName, $uri){ 
          return is_dir($sitePath.'/wp-admin');
          }

      isStaticFile Method

      isStaticFile It should be determined whether the incoming request is for a "static" file, such as images and stylesheets. If the file is static, this method should return the fully qualified path to the static file on disk. If the incoming request is not for a static file, this method should return false:

      /**
       * 确定即将到来的请求是否针对静态文件。
       *
       * @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} If the serves method is not for the incoming request And the isStaticFile method will be called only when the request URI is not / and true is returned.

      frontControllerPath Method

      frontControllerPath The method should return the complete "front controller" of your application. A qualified path, which is usually your "index.php" file or equivalent:

      /**
       * 获取对应用程序的前端控制器的完全解析路径。
       *
       * @param  string  $sitePath
       * @param  string  $siteName
       * @param  string  $uri
       * @return string
       */
       public function frontControllerPath($sitePath, $siteName, $uri){
           return $sitePath.'/public/index.php';
           }

      Local Driver

      If you want to customize the Valet driver for a single application, create a LocalValetDriver.php file in the root directory of the application. Your custom driver can extend the ValetDriver base class or inherit an existing application-specific driver, such as: LaravelValetDriver:

      class LocalValetDriver extends LaravelValetDriver{   
          /**
           * 确定驱动程序是否满足请求。
           *
           * @param  string  $sitePath
           * @param  string  $siteName
           * @param  string  $uri
           * @return bool
           */ 
            public function serves($sitePath, $siteName, $uri){ 
                   return true;    
                   }    
          /**
           * 获取对应用程序的前端控制器的完全解析路径。
           *
           * @param  string  $sitePath
           * @param  string  $siteName
           * @param  string  $uri
           * @return string
           */
           public function frontControllerPath($sitePath, $siteName, $uri){    
               return $sitePath.'/public_html/index.php';  
                 }
          }

      Other Valet commands

      CommandDescription
      valet forgetRun this command from a "resident" directory to remove others from the resident directory list
      valet pathsView all "resident" paths
      valet restartRestart the Valet daemon process
      valet startStart the Valet daemon process
      valet stopStop the Valet daemon
      valet uninstallComplete uninstalling the Valet daemon
      This article was first published on the LearnKu.com website.