1. Overview
Valet is a minimalist development environment for Mac, with no vagrant, Apache, Nginx, or/etc/hosts files, and you can even share your site publicly with a local tunnel.
In Mac, when you start the machine, Laravel valet always runs the PHP built-in Web server in the background and then proxies all requests to the *.dev domain name and points to the local machine-installed site by using Dnsmasq,valet. Such a fast laravel development environment only needs to occupy 7 m of memory.
Valet does not want to replace vagrant or homestead, but offers another option, more flexibility, speed, and a smaller memory footprint.
Valet provides us with the following software and tools:
- Laravel
- Lumen
- Statamic
- Craft
- Wordpress
- Jigsaw
- Static HTML
Of course, you can also extend valet by customizing the driver.
Valet Or Homestead
As you know, Laravel also provides another development environment homestead. The difference between homestead and valet is both the target audience and the local development approach. Homestead provides a complete Ubuntu virtual machine with an automated nginx configuration, and homestead is undoubtedly the best choice if you need a complete virtualized Linux development environment or a windows/linux operating system.
Valet only supports Macs and requires that PHP and database servers be installed locally, which can be easily implemented by using the Homebrew command (brew install PHP70 and Brew install mariadb). Valet provides a fast, local development environment with minimal resource consumption, and valet is the best choice if you only need php/mysql and don't need a full virtualized development environment.
Finally, valet and homestead are good helpers for configuring the local Laravel development environment and choosing which one to use depends on your personal preferences or the needs of your team.
2. Installation
Valet requires Mac operating system and Homebrew. Before installing, you need to ensure that there are no other programs such as Apache or Nginx bound to the local 80 port. The installation steps are as follows:
- Install or update homebrew to the latest version using Brew update
- By running the Brew Services list to ensure that brew services is valid and able to obtain the correct output, if it is not valid, you need to add it.
- Installing PHP 7.0:brew install php70 via homebrew.
- Install Valet:composer global require laravel/valet via composer (make sure ~/.composer/vendor/bin is in the system path)
- Run the Valet Install command, which configures and installs valet and DNSMASQ, and then registers valet background random boot.
After installing valet, try using a command such as Ping Foobar.dev to ping any *.dev domain name at the terminal, and if valet is installed correctly you will see a response from 127.0.0.1.
Each time the system starts, the valet background will start automatically, without having to manually run valet start or valet install again.
Database
If you need a database, you can install MARIADB through the Brew install MARIADB at the command line, and you can connect to the database with the user name root and a blank password when the installation is complete.
3. Service Site
Once the valet installation is complete, the service site can be started, and Valet provides two commands for this: Park and link
Park Command
- Create a new directory in your Mac, such as MkDir ~/sites, and then go to this directory and run Valet park. This command will use the current directory as the Web root directory.
- Next, create a new Laravel site in the new directory: Laravel new blog.
- Access the Http://blog.dev in the browser.
This is all the work we have to do. Is it convenient for all laravel projects created in the Sites directory to be accessed in the browser Http://folder-name.dev this way?
Link Command
The link command can also be used for a local Laravel site, which 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 that valet creates a symbolic link in ~/.valet/sites to the current working directory.
- After you run the link command, you can access it through Http://app-name.dev in the browser.
To view all the linked directories, you can run the valet Links command. You can also delete symbolic links by valet unlink app-name.
4. Share the site
Valet also provides a command to share the local site with others, which does not require any additional tools.
To share a site, switch to the directory where the station is located and run valet share, which generates a publicly accessible URL and inserts the Clipboard so that you can copy directly to the browser address bar, which is as simple as that.
To stop sharing a site, use Control + C.
5. View Logs
If you want to display the logs for all sites at the terminal, you can run the valet logs command, which displays the new logs at the terminal.
6. Custom Valet Driver
You can also write a custom valet driver to service PHP applications running on non-valet native support. When you finish installing valet, a ~/.valet/drivers directory is created with a samplevaletdriver.php file that shows an example of how to write a custom driver. Writing a driver requires only three methods: Serves, Isstaticfile, and Frontcontrollerpath.
These three methods receive $sitePath, $siteName, and $uri values as parameters, where $sitePath represents the site directory, such as/users/lisa/sites/my-project, $siteName represents the primary domain part, such as My-project, and $uri is the requested address entered, such as/foo/bar.
After you have written your custom valet driver, place it in the ~/.valet/drivers directory and follow Frameworkvaletdriver.php This naming method, for example, if you are writing custom valet drivers for WordPress, the corresponding file name is wordpressvaletdriver.php.
Let's discuss and demonstrate in detail the three methods that a custom valet driver needs to implement.
serves method
If the custom driver needs to continue processing the input request, the Serves method returns True, otherwise the method returns false. Therefore, you should determine whether a given $sitePath contains items of your service type in this method.
For example, suppose we write wordpressvaletdriver, then the corresponding serves method is as follows:
/** * Determine if the driver serves the request. * * @param string $sitePath * @param string $siteName * @param string $uri * @return void * @tran Slator laravelacademy.org */public function serves ($sitePath, $siteName, $uri) { return Is_dir ($sitePath. ' /wp-admin ');}
Isstaticfile Method
The Isstaticfile method determines whether the input request is a static file, a sample slice, or a style file, and if the file is static, the method returns the full path on the disk, or False if the input request is not a static file request:
/** * Determine if the incoming request is 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: The Isstaticfile method is called only if the Serves method returns true and the request URI is not/.
Frontcontrollerpath Method
The Frontcontrollerpath method returns the full path of the front 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 |
Description |
Valet Forget |
Run the command from the parked directory to remove the directory from the parked directory list |
Valet paths |
View your "parked" path |
Valet restart |
Restart Valet |
Valet Start |
Start Valet |
Valet Stop |
Close Valet |
Valet Uninstall |
Uninstalling Valet |