Laravel Homestead:设置快捷访问
全局可用的 Homestead 命令
日常使用中,我们使用 Vagrant 命令来管理 Homestead 虚拟机,例如常用的 vagrant up 命令启动虚拟机。可 Vagrant 命令必须在放置虚拟机配置信息的文件夹中 ~/Homestead 执行,否则会报类似以下的错误:
A Vagrant environment or target machine is required to run this
command. Runvagrant initto create a new Vagrant environment. Or,
get an ID of a target machine fromvagrant global-statusto run
this command on. A final option is to change to a directory with a
Vagrantfile and to try again.
解决方法是设置 homestead 命令行快捷访问,这样的话在任意路径都能运行 vagrant up 命令来启动 Homestead 虚拟机。
接下来分别讲解。
Mac / Linux
在 Mac / Linux 系统上可以在 Bash 配置文件( ~/.bash_profile )中添加 Bash 函数 。
function homestead() {
( cd ~/Homestead && vagrant $* )
}
确保将该函数中的 ~/Homestead 路径调整为实际的 Homestead 安装路径。这样你就可以在系统的任意位置运行 homestead up 或 homestead ssh 等命令。
Windows
在 Windows 上, 可以添加「批处理」文件到 PATH 。在系统的任意位置创建一个批处理文件 homestead.bat,内容如下:
@echo off
set cwd=%cd%
set homesteadVagrant=C:\Homestead
cd /d %homesteadVagrant% && vagrant %*
cd /d %cwd%
set cwd=
set homesteadVagrant=
一定要将脚本中 C:\Homestead 路径调整为 Homestead 的实际安装路径。创建文件后,将文件路径添加到 PATH。然后你就可以在系统任意位置运行 homestead up 或 homestead ssh 等命令。
大功告成

Laravel 社区 Wiki
关于 LearnKu
推荐文章: