1、概述
Valet是为Mac提供的极简主义开发环境,没有Vagrant、Apache、Nginx,也没有 /etc/hosts文件,甚至可以使用本地隧道公开共享你的站点。
在Mac中,当你启动机器时,Laravel Valet总是在后台运行PHP内置的Web服务器,然后通过使用 DnsMasq,Valet将所有请求代理到 *.dev域名并指向本地机器安装的站点。这样一个极速的Laravel开发环境只需要占用7M内存。
Valet并不是想要替代Vagrant或者Homestead,只是提供了另外一种选择,更加灵活、极速、以及占用更小的内存空间。
Valet为我们提供了以下软件和工具:
- Laravel
- Lumen
- Statamic
- Craft
- WordPress
- Jigsaw
- 静态HTML
当然,你还可以通过自定义的驱动扩展Valet。
Valet Or Homestead
正如你所知道的,Laravel 还提供了另外一个开发环境Homestead。Homestead和Valet的不同之处在于两者的目标受众和本地开发方式。Homestead提供了一个完整的包含自动化Nginx配置的Ubuntu虚拟机,如果你需要一个完整的虚拟化Linux开发环境或者使用的是Windows/Linux操作系统,那么Homestead无疑是最佳选择。
Valet只支持Mac,并且要求本地安装了PHP和数据库服务器,这可以通过使用 Homebrew命令轻松实现( brew install php70以及 brew install mariadb),Valet通过最小的资源消耗提供了一个极速的本地开发环境,如果你只需要PHP/MySQL并且不需要完整的虚拟化开发环境,那么Valet将是最好的选择。
最后,Valet和Homestead都是配置本地Laravel开发环境的好帮手,选择使用哪一个取决于你个人的喜好或团队的需求。
2、安装
Valet要求Mac操作系统和 Homebrew。安装之前,需要确保没有其他程序如Apache或Nginx绑定到本地的80端口。安装步骤如下:
- 使用 brew update安装或更新Homebrew到最新版本
- 通过运行 brew services list确保 brew services有效并且能获取到正确的输出,如果无效,则需要 添加。
- 通过Homebrew安装PHP 7.0: brew install php70。
- 通过Composer安装Valet: composer global require laravel/valet(确保 ~/.composer/vendor/bin在系统路径中)
- 运行 valet install命令,这将会配置并安装Valet和DnsMasq,然后注册Valet后台随机启动。
安装完Valet后,尝试使用命令如 ping foobar.dev在终端ping一下任意 *.dev域名,如果Valet安装正确就会看到来自 127.0.0.1的响应。
每次系统启动的时候Valet后台会自动启动,而不需要再次手动运行 valet start或 valet install。
数据库
如果你需要数据库,可以在命令行通过 brew install mariadb安装MariaDB,安装完成后就可以在本机通过用户名 root和一个空密码连接到数据库。
3、服务站点
Valet安装完成后,就可以启动服务站点,Valet为此提供了两个命令: park和 link
park命令
- 在Mac中创建一个新目录,例如 mkdir ~/Sites,然后进入这个目录并运行 valet park。这个命令会将当前所在目录作为web根目录。
- 接下来,在新建的目录中创建一个新的Laravel站点: laravel new blog。
- 在浏览器中访问 http://blog.dev。
这就是我们要做的全部工作。现在,所有在 Sites目录中创建的Laravel项目都可以通过 http://folder-name.dev这种方式在浏览器中访问,是不是很方便?
link命令
link命令也可以用于本地Laravel站点,这个命令在你想要在目录中提供单个站点时很有用。
- 要使用这个命令,先切换到你的某个项目并运行 valet link app-name,这样Valet会在 ~/.valet/Sites中创建一个符号链接指向当前工作目录。
- 运行完 link命令后,可以在浏览器中通过 http://app-name.dev访问。
要查看所有的链接目录,可以运行 valet links命令。你也可以通过 valet unlink app-name来删除符号链接。
4、分享站点
Valet还提供了一个命令用于将本地站点共享给其他人,这不需要任何额外工具即可实现。
要共享站点,切换到站点所在目录并运行 valet share,这会生成一个可以公开访问的URL并插入剪贴板,以便你直接复制到浏览器地址栏,就是这么简单。
要停止共享站点,使用 Control + C即可。
5、查看日志
如果你想要在终端显示所有站点的日志,可以运行 valet logs命令,这会在终端显示新的日志。
6、自定义Valet驱动
你还可以编写自定义的Valet驱动为运行于非Valet原生支持的PHP应用提供服务。安装完Valet时会创建一个 ~/.valet/Drivers目录,该目录中有一个 SampleValetDriver.php文件,这个文件中有一个演示如何编写自定义驱动的示例。编写一个驱动只需要实现三个方法: serves、 isStaticFile和 frontControllerPath。
这三个方法接收 $sitePath、 $siteName和 $uri值作为参数,其中 $sitePath表示站点目录,如 /Users/Lisa/Sites/my-project, $siteName表示主域名部分,如 my-project,而 $uri则是输入的请求地址,如/foo/bar。
编写好自定义的Valet驱动后,将其放到 ~/.valet/Drivers目录并遵循 FrameworkValetDriver.php这种命名方式,举个例子,如果你是在为Wordpress编写自定义的valet驱动,对应的文件名称为 WordPressValetDriver.php。
下面我们来具体讨论并演示自定义Valet驱动需要实现的三个方法。
serves方法
如果自定义驱动需要继续处理输入请求, serves方法会返回 true,否则该方法返回 false。因此,在这个方法中应该判断给定的 $sitePath是否包含你服务类型的项目。
例如,假设我们编写的是 WordPressValetDriver,那么对应 serves方法如下:
/** * 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方法
isStaticFile方法会判断输入请求是否是静态文件,例如图片或样式文件,如果文件是静态的,该方法会返回磁盘上的完整路径,如果输入请求不是请求静态文件,则返回 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;}
注: isStaticFile方法只有在 serves方法返回 true并且请求URI不是 /的时候才会被调用。
frontControllerPath方法
frontControllerPath方法会返回前端控制器的完整路径,通常是 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、其他Valet命令
命令 | 描述 |
---|---|
valet forget | 从”parked”目录运行该命令以便从parked目录列表中移除该目录 |
valet paths | 查看你的”parked”路径 |
valet restart | 重启Valet |
valet start | 启动Valet |
valet stop | 关闭Valet |
valet uninstall | 卸载Valet |

In PHP, trait is suitable for situations where method reuse is required but not suitable for inheritance. 1) Trait allows multiplexing methods in classes to avoid multiple inheritance complexity. 2) When using trait, you need to pay attention to method conflicts, which can be resolved through the alternative and as keywords. 3) Overuse of trait should be avoided and its single responsibility should be maintained to optimize performance and improve code maintainability.

Dependency Injection Container (DIC) is a tool that manages and provides object dependencies for use in PHP projects. The main benefits of DIC include: 1. Decoupling, making components independent, and the code is easy to maintain and test; 2. Flexibility, easy to replace or modify dependencies; 3. Testability, convenient for injecting mock objects for unit testing.

SplFixedArray is a fixed-size array in PHP, suitable for scenarios where high performance and low memory usage are required. 1) It needs to specify the size when creating to avoid the overhead caused by dynamic adjustment. 2) Based on C language array, directly operates memory and fast access speed. 3) Suitable for large-scale data processing and memory-sensitive environments, but it needs to be used with caution because its size is fixed.

PHP handles file uploads through the $\_FILES variable. The methods to ensure security include: 1. Check upload errors, 2. Verify file type and size, 3. Prevent file overwriting, 4. Move files to a permanent storage location.

In JavaScript, you can use NullCoalescingOperator(??) and NullCoalescingAssignmentOperator(??=). 1.??Returns the first non-null or non-undefined operand. 2.??= Assign the variable to the value of the right operand, but only if the variable is null or undefined. These operators simplify code logic, improve readability and performance.

CSP is important because it can prevent XSS attacks and limit resource loading, improving website security. 1.CSP is part of HTTP response headers, limiting malicious behavior through strict policies. 2. The basic usage is to only allow loading resources from the same origin. 3. Advanced usage can set more fine-grained strategies, such as allowing specific domain names to load scripts and styles. 4. Use Content-Security-Policy-Report-Only header to debug and optimize CSP policies.

HTTP request methods include GET, POST, PUT and DELETE, which are used to obtain, submit, update and delete resources respectively. 1. The GET method is used to obtain resources and is suitable for read operations. 2. The POST method is used to submit data and is often used to create new resources. 3. The PUT method is used to update resources and is suitable for complete updates. 4. The DELETE method is used to delete resources and is suitable for deletion operations.

HTTPS is a protocol that adds a security layer on the basis of HTTP, which mainly protects user privacy and data security through encrypted data. Its working principles include TLS handshake, certificate verification and encrypted communication. When implementing HTTPS, you need to pay attention to certificate management, performance impact and mixed content issues.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Dreamweaver CS6
Visual web development tools

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

Zend Studio 13.0.1
Powerful PHP integrated development environment

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment