valet mac_将Laravel Valet用于Super Quick Dev Server

valet mac

From the early days of tedious drills to set up your development environment, things have moved way forward.

从繁琐的钻研初期到建立您的开发环境,事情已经向前发展。

We had early signs of the direction things were going in with the advent of MAMP. Today, we have come to a point where we have portable, virtual boxes that can be used to develop and deploy your applications.

随着MAMP的到来,我们早已知道事情的发展方向。 如今,我们已经有了一个可移植的虚拟盒子,可用于开发和部署您的应用程序。

The newest addition to the I want to set it up quickly movement is Laravel Valet.

我想快速设置运动的最新成员是Laravel Valet

Though it's used in a slightly different manner, it still scores if you wish to set up your development environment in a zap!

尽管使用方式略有不同,但是如果您希望在zap中设置开发环境,它仍然可以得分!

During the course of this tutorial, you will learn.

在本教程的过程中,您将学习。

  1. What is Laravel Valet and how it works.

    什么是Laravel Valet及其运作方式。
  2. How it compares to Homestead/Scotch Box and their use cases.

    与Homestead / Scotch Box及其用例的比较。
  3. The complete Valet command set.

    完整的代客命令集。
  4. How to use a database with Valet.

    如何在代客使用数据库。
  5. Sharing your application with the world(yes, you can do that with Valet)

    与世界共享您的应用程序(是的,您可以通过代客完成)

At the time of writing this article, Valet is only available for OSX. If you do not have OSX available, you are limited to Windows and *nix based options and the discussion that follows does not apply.

在撰写本文时,Valet仅适用于OSX。 如果没有可用的OSX,则只能使用基于Windows和* nix的选项,并且下面的讨论将不适用。

Laravel Valet-简介 ( Laravel Valet - A Brief Introduction )

Laravel Valet is a development environment that runs without Nginx and Apache. Yes, read that again.

Laravel Valet是一个无需NginxApache即可运行的开发环境。 是的,再读一遍。

It's a layer written upon the Caddy server to make setting up a Laravel environment easier.

这是在Caddy服务器上编写的一层,以简化Laravel环境的设置。

Plus, it also employs DnsMasq to route requests to sites on your localhost so it takes the need to configure /etc/hosts and virtual hosts out of the equation.

另外,它还使用DnsMasq将请求路由到本地主机上的站点,因此无需配置/etc/hosts和虚拟主机。

Homestead and Scotch Box are very popular options for setting up a Laravel development environment. Let's see how they compare to Valet.

HomesteadScotch Box是设置Laravel开发环境的非常受欢迎的选项。 让我们看看他们与代客的比较。

宅基,苏格兰箱和代客餐 ( Homestead, Scotch Box, and Valet )

Homestead and Scotch Box are portable development environments powered by Vagrant.

HomesteadScotch Box是由Vagrant支持的便携式开发环境。

Vagrant itself is another software layer built on top of hardware virtualization tools like VMWare Workstation and VirtualBox.

Vagrant本身是在硬件虚拟化工具(如VMWare WorkstationVirtualBox)之上构建的另一个软件层。

It has automation infrastructure built into it as it supports provisioning tools like shell scripts, Chef, and Puppet that you can use to build pre-packaged, pre-configured virtual machines.

它具有内置的自动化基础架构,因为它支持可用于构建预打包,预配置的虚拟机的置备工具,例如Shell脚本, ChefPuppet

Though Vagrant runs purely from the command line, underneath, it uses VMWare Workstation, VirtualBox(or other hardware virtualization software) to create an isolated, virtual environment on your local machine that is used to run your applications.

尽管Vagrant仅在命令行下运行,但它使用VMWare Workstation,VirtualBox(或其他硬件虚拟化软件)在用于运行应用程序的本地计算机上创建隔离的虚拟环境。

Further, inside the virtual environment, you have softwares like Apache, and MySQL pre-installed.

此外,在虚拟环境中,您已预安装了Apache和MySQL之类的软件。

Valet, on the other hand, buys into a completely different scheme of things.

另一方面,代客购买完全不同的方案。

It's installed on your physical machine and uses the Caddy server and DnsMasq to re-route requests to locally stored websites.

它安装在您的物理计算机上,并使用Caddy服务器和DnsMasq将请求重新路由到本地存储的网站。

Bear in mind, if you opt for Valet, you still need to install pre-requisite softwares such as MySQL and RabbitMQ if they are required by your application.

请记住,如果选择代客,如果您的应用程序需要它们,则仍然需要安装必备软件,例如MySQL和RabbitMQ

宅基地/斯科特盒子和代客使用案例 ( Homestead/Scoth Box and Valet Use Cases )

If you have understood the basic difference between Homestead/Scotch Box and Valet, the obvious question would be how their usage fits into different scenarios.

如果您了解Homestead / Scotch Box和Valet之间的基本区别,那么显而易见的问题是它们的用法如何适应不同的场景。

Valet is a lot lighter option than the mentioned Vagrant boxes so if computational resources are an issue, in that case, you are better off going with Valet.

Valet比提到的Vagrant框要轻得多,因此,如果计算资源成问题,那么最好还是使用Valet。

Moreover, Valet is more suited to individuals. It's a nice alternative for solo artists who wish to steer clear of heavy configurations and software installation.

此外,代客更适合个人。 对于希望避免繁重的配置和软件安装的独奏艺术家来说,这是一个不错的选择。

It can also serve to be quite a neat option to demo projects in quick client meetings.

在快速的客户会议中演示项目时,它也可以是一个很好的选择。

For maximum portability, and in cases where your environment needs to be re-produced multiple times, say while working within a team, my suggestion would be to use Homestead or Scotch Box.

为了获得最大的可移植性,并且在需要多次复制环境的情况下(例如在团队中工作时),我的建议是使用Homestead或Scotch Box。

In a replicate across multiple machines scenario, Valet would need some work to set things up plus, remember it's OSX only?

跨多台机器复制的场景中,代客需要做一些工作来进行设置,还记得它仅是OSX吗?

Homestead and Scotch Box also open you up to deployment options. Generally speaking, Vagrant facilitates application deployment(through FTP, Heroku, and other options) using a single push command.

Homestead和Scotch Box还为您提供了部署选项。 一般来说,Vagrant使用单个push命令即可促进应用程序部署(通过FTP, Heroku和其他选项)。

In case of Valet, deployment won't be a simple task. For a start, you will need to setup Caddy server on your production machine.

如果是代客泊车,部署将不是一件容易的事。 首先,您需要在生产计算机上设置Caddy服务器。

If you are still unsure, I would recommend going for Homestead/Scotch Box as it's a more professional, well-rounded option.

如果您仍然不确定,我建议您选择Homestead / Scotch Box,因为它是更专业,更全面的选择。

Valet is a rather personal, compact option. If those traits ring a bell, it's right down your alley.

代客停车是一种非常个人化的紧凑型选择。 如果这些特征敲响了,那就在你的巷子里。

安装先决条件 ( Installing the Pre-Requisites )

In order to install Valet, you should have Homebrew, PHP 7, and Composer installed.

为了安装Valet,您应该安装HomebrewPHP 7Composer

安装自制软件 (Installing Homebrew)

Homebrew is a package manager for OSX just like apt, yum, and pacman. You can read about it if you are interested in the details.

Homebrew是OSX的软件包管理器,就像aptyumpacman 。 如果您对这些细节感兴趣,可以阅读一下。

Execute the following command in your terminal to install Homebrew.

在终端中执行以下命令以安装Homebrew。

# install homebrew
$ /usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"

The script will prompt you through the process so read and follow the steps carefully.

该脚本将提示您完成该过程,因此请仔细阅读并按照步骤进行操作。

安装PHP 7 (Installing PHP 7)

Valet requires PHP's version 7.0 or greater.

代客需要PHP 7.0或更高版本。

In order to find out your current PHP version, execute the following command in your terminal.

为了找出您当前PHP版本,请在终端中执行以下命令。

# view php version
$ php -v

If the output shows a version less than 7.0 than you will need to update your PHP version.

如果输出显示的版本低于7.0,则需要更新PHP版本。

Execute the following command to install PHP 7.0.

执行以下命令以安装PHP 7.0。

# install php 7 using homebrew
$ brew install homebrew/php/php70

Restart your terminal session, the output of the php -v command should now echo version 7 for PHP.

重新启动终端会话,现在php -v命令的输出应回显PHP的版本7。

安装作曲家 (Installing Composer)

Lastly, install Composer globally by following the instructions at the official website.

最后,按照官方网站上的说明在全球范围内安装Composer。

When you are done, you should be able to access Composer from anywhere in your terminal.

完成后,您应该可以从终端的任何位置访问Composer。

You can verify Composer's global installation by executing the following command.

您可以通过执行以下命令来验证Composer的全局安装。

# view composer version
$ composer -v

If the output of the command shows the Composer version, it was set up correctly.

如果命令的输出显示Composer版本,则说明已正确设置。

Also, make sure Composer's bin directory is added to your system PATH. You can do that by appending the following line at the end of your ~/.profile file.

另外,确保将Composer的bin目录添加到系统PATH 。 您可以通过在~/.profile文件末尾添加以下行来实现。

If you're on Windows, make sure you have the path (C:\Users\<Username>\AppData\Roaming\Composer\vendor\bin) to composer in your environment variables.

如果您使用的是Windows,请确保环境变量中包含 composer的路径( C:\Users\<Username>\AppData\Roaming\Composer\vendor\bin )。

export PATH="$PATH:$HOME/.composer/vendor/bin" # Add composer bin directory to PATH 

Once you have Homebrew, PHP, and Composer installed, you can proceed to installing Valet.

一旦安装了Homebrew,PHP和Composer,就可以继续安装Valet。

安装代客 ( Installing Valet )

Before installing Valet, make sure there is no application installed and running that uses port 80. Generally, web servers such as Apache and Nginx use port 80 which will be required by Valet to run the Caddy server. If you have any service(or web server) configured to run on port 80, you should disable it.

在安装Valet之前,请确保没有安装和运行使用端口80的应用程序。通常,Web服务器(例如Apache和Nginx)使用端口80,Valet将需要该端口运行Caddy服务器。 如果您将任何服务(或Web服务器)配置为在端口80上运行,则应禁用它。

Execute the following command to query any running services on port 80.

执行以下命令以查询端口80上所有正在运行的服务。

# check if an application
# is using port 80
$ netstat -an | grep "\.80" | grep LISTEN

If the command produced empty output, port 80 is clean, and you are good to proceed with Valet installation.

如果命令产生的输出为空,则端口80是干净的,您可以继续进行代客安装。

Execute the following command to require Valet globally.

执行以下命令以全局要求代客。

# install valet package using composer
$ composer global require laravel/valet

Next, install Valet.

接下来,安装代客。

# run valet setup
$ valet install

If Valet was installed correctly, you will be prompted with the Valet installed successfully! message.

如果代客安装正确,系统将提示您Valet installed successfully! 信息。

关于代客安装的额外说明 (An Extra Word on Valet Installation)

Despite following the aforementioned installation steps, you may run into a funny [DomainException] error as shown in the screenshot.

尽管执行了上述安装步骤,您仍可能会遇到一个有趣的[DomainException]错误,如屏幕截图所示。

Valet installation error

If that is the case, update Homebrew and upgrade Homebrew formulas first.

如果是这种情况,请先更新Homebrew并升级Homebrew公式。

# update homebrew
$ brew update
# upgrade homebrew formulas
$ brew upgrade

Next, install DnsMasq using Homebrew.

接下来,使用Homebrew安装DnsMasq。

# install dnsmasq using homebrew
$ brew install dnsmasq

Then proceed with Valet installation using the valet install command.

然后,使用valet install命令继续进行代客安装。

使用Laravel Valet ( Using Laravel Valet )

Phew! we have got Valet installation out of the way so let's get to seeing it in action.

! 我们已经完成了代客安装,因此让我们来看一下实际应用。

控制代客守护程序 (Controlling the Valet Daemon)

The Valet installation configures a daemon to run automatically when your system boots.

代客安装将守护程序配置为在系统引导时自动运行。

This makes sure all your sites(that have been setup) are served without re-configuration or manually starting the web server.

这样可确保无需重新配置或手动启动Web服务器即可为所有站点(已设置的站点)提供服务。

However, you can jump into the automated process and do as you like with the Valet daemon.

但是,您可以跳入自动化过程,并根据需要使用Valet守护程序进行操作。

You can execute the following commands to control the Valet daemon.

您可以执行以下命令来控制Valet守护程序。

# restart the Valet daemon
$ valet restart
# start the Valet daemon
$ valet start
# stop the Valet daemon
$ valet stop

Valet Daemon

If you wish to empty out port 80 and work with other web servers, these commands can come in handy.

如果您希望清空端口80并与其他Web服务器一起使用,可以使用这些命令。

停车/停车目录 (Parking/Unparking Directories)

The park command allows you to add a directory to Valet's path so that Laravel sites can be automatically detected and served from it.

park命令允许您向Valet的路径添加目录,以便可以自动检测Laravel网站并从中提供服务。

Let's say you keep all your Laravel projects at ~/projects/laravel/.

假设您将所有Laravel项目保留在~/projects/laravel/

In that case, here is how you would park this directory.

在这种情况下,这是您如何park此目录的方法。

# move to the laravel projects directory
$ cd ~/projects/laravel/
# execute the park command inside the directory
$ valet park

Valet Park Command

Valet's auto-generated URLs follow the convention http://directory-name.dev.

代客自动生成的URL遵循约定http://directory-name.dev

For instance, let's assume you had the following directories inside the ~/projects/laravel/ directory as shown in the screenshot.

例如,假设您在~/projects/laravel/目录中具有以下目录,如屏幕截图所示。

Projects Directory

In that case, your Laravel projects would be available at the URLs http://bar.dev, http://blog.dev, http://foo.dev, http://marvel.dev, http://pastebin.dev, http://snappy.dev, and http://zoo-tycoon.dev respectively.

在这种情况下,你的Laravel项目将可在网址http://bar.devhttp://blog.devhttp://foo.devhttp://marvel.devhttp://pastebin.devhttp://snappy.dev http://pastebin.devhttp://zoo-tycoon.dev

The park command is a nice, time-saving shortcut if you keep all your Laravel projects in one place.

如果将所有Laravel项目放在一个地方,则park命令是一个不错的省时捷径。

The forget command does the opposite of the park command.

forget命令执行与park命令相反的操作。

If you wish to remove a directory from the list of parked directories, here is how to do it.

如果要从停放目录列表中删除目录,请按照以下步骤操作。

# move to the laravel projects directory
$ cd ~/projects/laravel/
# execute the forget command inside
# the directory to unpark it
$ valet forget

Valet Forget Command

If you wish to view the list of parked directories, you can execute the following command.

如果要查看暂存目录的列表,可以执行以下命令。

# list parked directories
$ valet paths

Valet Paths Command

链接/取消链接目录 (Linking/Unlinking Directories)

It could be that your projects directory contains applications of different race and color(like Ruby, Python, and Java). In such a case, it may not be wise to park the whole directory as non-PHP applications, though parked, will be of no use.

您的项目目录可能包含不同种族和颜色的应用程序(例如Ruby,Python和Java)。 在这种情况下,将整个目录停放可能不是明智的选择,因为非PHP应用程序虽然可以停放,但没有用。

In such a case, you can use the link command to symbolically link a single directory to Valet.

在这种情况下,您可以使用link命令将单个目录符号链接到代客。

For example, re-visiting the previous screenshot, assume marvel was the only Laravel application in your directory.

例如,重新访问之前的屏幕快照,假设marvel是您目录中唯一的Laravel应用程序。

You could link it like so.

您可以像这样链接它。

# move to the marvel directory
$ cd ~/projects/laravel/marvel/
# link the marvel directory to
# make it accessible at http://<app-name>.dev
$ valet link <app-name>
# example: valet link marvel

Valet Link Command

As previously mentioned, your application will be accessible at http://marvel.dev.

如前所述,可以在http://marvel.dev上访问您的应用程序。

You can also unlink directories by following the same routine, the only difference is the keywork unlink.

您还可以按照相同的例程来取消目录的链接,唯一的区别是键盘unlink

# move to the marvel directory
$ cd ~/projects/laravel/marvel/
# unlink the marvel directory
$ valet unlink <app-name>
# example: valet unlink marvel

Valet Unlink Command

Also be sure to provide the same <app-name> when unlinking that you provided when linking.

还要确保在取消链接时提供与链接时提供的相同的<app-name>

If you wish to view the list of linked directories, you can execute the following command.

如果要查看链接目录的列表,可以执行以下命令。

# list linked directories
$ valet links

Valet Links Command

更改默认域后缀 (Changing Default Domain Suffix)

Valet's default domain schema follows the convention http://<directory-name>.dev.

代客的默认域架构遵循约定http://<directory-name>.dev

If you wish to change the domain's dev suffix, you can do so by executing the domain command.

如果要更改域的dev后缀,可以通过执行domain命令来进行。

# make all local sites
# available at http://<directory-name>.<suffix> 
$ valet domain <suffix>
# example: valet domain abc

Valet Domain Command

All your local sites will now be accessible at http://<directory-name>.<suffix>.

现在可以在http://<directory-name>.<suffix>上访问所有本地站点。

流日志到终端 (Streaming Logs to Terminal)

Though your Laravel application's logs are written to disk and you can skim through them at your leisure, sometimes you wish to look at things as they happen.

尽管您的Laravel应用程序的日志已写入磁盘,并且您可以在闲暇时浏览它们,但有时您希望在发生时查看情况。

In such a case, you can instruct Valet to stream your logs to the terminal using the logs command.

在这种情况下,可以指示Valet使用logs命令将logs流式传输到终端。

# puke all logs to terminal
$ valet logs

与世界分享您的网站 (Sharing Your Site With the World)

Brace yourselves, you are about to witness the coolest feature of Valet.

振作起来,您将见证Valet最酷的功能。

Earlier, we saw how you can set up local sites using the park and link commands.

之前,我们看到了如何使用parklink命令设置本地站点。

Once you have set up a local site using either of these commands, you can create a sharing link that can be used to share your website with the world(read: anyone who has a internet connection).

使用以下两个命令之一设置本地站点后,您就可以创建一个共享链接,该链接可用于与世界共享您的网站(阅读:具有互联网连接的任何人)。

Assuming you have linked the marvel directory using the link command, here is how to create a sharing link.

假设您已经使用link命令链接了marvel目录,这是创建共享链接的方法。

# move to the marvel directory
$ cd ~/projects/laravel/marvel/
# create a share link
$ valet share

A new window will be initialized with a new process. Your site will be accessible as long as the process keeps running. You can terminate the process by pressing ctrl+C.

新窗口将使用新进程初始化。 只要过程继续运行,您的网站就可以访问。 您可以通过按ctrl+C终止该过程。

Also, you can find the share url for your site next to the forwarding section. I have highlighted the sharing url generated in my case in the following screenshot.

另外,您可以在转发部分旁边找到您网站的共享网址。 在下面的屏幕快照中,我突出显示了在我的情况下生成的共享URL。

Valet Share Command

Slam Dunkin代客 (Slam Dunkin Valet)

Despite the convenience that Valet provides, there is still a very microscopic chance that for some reason, you do not like it.

尽管Valet提供了便利,但由于某种原因,您仍然很可能不喜欢它。

If you want to remove Valet, execute the following command.

如果要删除代客,请执行以下命令。

# uninstall Valet
$ valet uninstall

And you are done(or undone?).

您完成了(或撤消了?)。

与代客使用数据库 ( Using a Database with Valet )

So far, so good.

到目前为止,一切都很好。

You have learned the complete command set of Valet and must be wondering, how do I use a database with this thing.

您已经了解了Valet的完整命令集,并且一定在想,我该如何使用数据库。

Recall, in the earlier section where I compared Valet to Homestead/Scotch Box, I mentioned that if you opt to use Valet, you will need to install pre-requisite software required by your application on your physical machine.

回想一下,在之前将Valet与Homestead / Scotch Box进行比较的部分中,我提到过,如果您选择使用Valet,则需要在物理计算机上安装应用程序所需的必备软件。

A database server (MySQL/MariaDB) falls under the umbrella of your app's requirements and you can install it using Homebrew.

数据库服务器( MySQL / MariaDB )属于您应用程序要求的保护范围,您可以使用Homebrew进行安装。

# install MySQL
$ brew install mysql
# or install MariaDB
$ brew install mariadb

Once you have MySQL or MariaDB installed, simply configure your Laravel application with the correct database settings and it should work fine with Valet.

一旦安装了MySQL或MariaDB,只需使用正确的数据库设置配置Laravel应用程序,它就可以与Valet一起正常工作。

That's it. There are no extra, Valet-specfic steps needed.

而已。 无需其他特定于代客的步骤。

最后的想法 ( Final Thoughts )

Valet is a great option for Mac minimalists.

对于Mac极简主义者,代客是一个不错的选择。

If you are the kind of designer/developer who does not work much with application infrastructure and is only concerned with application delivery, Valet can even serve to be a long term option.

如果您是那种在应用程序基础架构上工作不多而仅关注应用程序交付的设计师/开发人员,那么Valet甚至可以作为一个长期选择。

I recommend diving into the Caddy server if Valet really interested you. Plus, you can also look into developing Valet drivers if you are up for a weekend coding challenge.

如果Valet真的很感兴趣,我建议您进入Caddy服务器。 另外,如果您准备周末编码挑战,也可以研究开发Valet驱动程序

I hope you found this tutorial interesting and knowledgeable. Until my next piece, happy coding!

我希望您发现本教程有趣且知识丰富。 直到我的下一篇文章,祝您编程愉快!

翻译自: https://scotch.io/tutorials/use-laravel-valet-for-a-super-quick-dev-server

valet mac

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值