Laravel Homestead:设置快捷访问 1 个改进

全局可用的 Homestead 命令

日常使用中,我们使用 Vagrant 命令来管理 Homestead 虚拟机,例如常用的 vagrant up 命令启动虚拟机。可 Vagrant 命令必须在放置虚拟机配置信息的文件夹中 ~/Homestead 执行,否则会报类似以下的错误:

A Vagrant environment or target machine is required to run this
command. Run vagrant init to create a new Vagrant environment. Or,
get an ID of a target machine from vagrant global-status to 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 uphomestead 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 uphomestead ssh 等命令。

大功告成

设置快捷访问

本文为 Wiki 文章,邀您参与纠错、纰漏和优化
讨论数量: 2

这个很好, 不用每次在进入homestead目录了

5年前 评论
elesos

宁愿进目录,也不搞win cmd

4年前 评论

讨论应以学习和精进为目的。请勿发布不友善或者负能量的内容,与人为善,比聪明更重要!