Readouble

Laravel 5.7 Laravel Valet

イントロダクションIntroduction

Valet(ベレット:従者)はMacミニマニストのためのLaravel開発環境です。Vagrantも不要、/etc/hostsファイルも不要です。更に、ローカルトンネルを使って、サイトを公開し、シェアすることもできます。ええ、私達はこういうのも好きなんですよね。Valet is a Laravel development environment for Mac minimalists. No Vagrant, no /etc/hosts file. You can even share your sites publicly using local tunnels. Yeah, we like it too.

Laravel Valetはマシン起動時にバックグランドでNginxがいつも実行されるように、Macを設定します。そのため、DnsMasqを使用し、Valetは*.testドメインへの全リクエストを、ローカルマシンへインストールしたサイトへ向けるようにプロキシ動作します。Laravel Valet configures your Mac to always run Nginx[https://www.nginx.com/] in the background when your machine starts. Then, using DnsMasq[https://en.wikipedia.org/wiki/Dnsmasq], Valet proxies all requests on the *.test domain to point to sites installed on your local machine.

言い換えれば、大体7MBのRAMを使う、とても早いLaravelの開発環境です。ValetはVagrantやHomesteadを完全に置き換えるものではありませんが、柔軟な基礎、特にスピード重視であるか、RAMが限られているマシンで動作させるのには、素晴らしい代替になります。In other words, a blazing fast Laravel development environment that uses roughly 7 MB of RAM. Valet isn't a complete replacement for Vagrant or Homestead, but provides a great alternative if you want flexible basics, prefer extreme speed, or are working on a machine with a limited amount of RAM.

Valetは以下をサポートしていますが、これらに限定されません。Out of the box, Valet support includes, but is not limited to:

- [Laravel](https://laravel.com) - [Lumen](https://lumen.laravel.com) - [Bedrock](https://roots.io/bedrock/) - [CakePHP 3](https://cakephp.org) - [Concrete5](https://www.concrete5.org/) - [Contao](https://contao.org/en/) - [Craft](https://craftcms.com) - [Drupal](https://www.drupal.org/) - [Jigsaw](https://jigsaw.tighten.co) - [Joomla](https://www.joomla.org/) - [Katana](https://github.com/themsaid/katana) - [Kirby](https://getkirby.com/) - [Magento](https://magento.com/) - [OctoberCMS](https://octobercms.com/) - [Sculpin](https://sculpin.io/) - [Slim](https://www.slimframework.com) - [Statamic](https://statamic.com) - Static HTML - [Symfony](https://symfony.com) - [WordPress](https://wordpress.org) - [Zend](https://framework.zend.com)

独自のカスタムドライバでValetを拡張できます。However, you may extend Valet with your own custom drivers[#custom-valet-drivers].

ValetとHomesteadValet Or Homestead

ご存知のように、ローカルのLaravel開発環境としてHomesteadも用意しています。HomesteadとValetは利用者の目的とローカルの開発についてのアプローチが異なります。Homesteadは自動的にNginx設定を行うUbuntuの完全な仮想マシンを提供しています。HomesteadはLinux開発環境の完全な仮想化を行いたい、もしくはWindows/Linux上で動作させたい場合、素晴らしい選択肢になります。As you may know, Laravel offers Homestead[/docs/{{version}}/homestead], another local Laravel development environment. Homestead and Valet differ in regards to their intended audience and their approach to local development. Homestead offers an entire Ubuntu virtual machine with automated Nginx configuration. Homestead is a wonderful choice if you want a fully virtualized Linux development environment or are on Windows / Linux.

ValetはMac上でのみサポートされ、PHPとデータベースサーバを直接ローカルマシンへインストールする必要があります。Homebrewを利用し、brew install phpbrew install mysqlのようなコマンドを実行すれば、簡単にインストールできます。Valetは最低限度のリソースを使い、とても早いローカル開発環境を提供します。そのため、PHPとMySQLだけが必要で、完全な仮想開発環境は必要ない場合にぴったりです。Valet only supports Mac, and requires you to install PHP and a database server directly onto your local machine. This is easily achieved by using Homebrew[https://brew.sh/] with commands like brew install php and brew install mysql. Valet provides a blazing fast local development environment with minimal resource consumption, so it's great for developers who only require PHP / MySQL and do not need a fully virtualized development environment.

ValetとHomesteadのどちらを選んでも、Laravelの開発環境に向け設定されており、良い選択になるでしょう。どちらを選ぶかは、自分の好みとチームの必要により決まるでしょう。Both Valet and Homestead are great choices for configuring your Laravel development environment. Which one you choose will depend on your personal taste and your team's needs.

インストールInstallation

ValetにはMacオペレーティングシステムとHomebrewが必要です。インストールする前に、ApacheやNginxのようなローカルマシンの80番ポートへバインドするプログラムがないことを確認してください。Valet requires macOS and Homebrew[https://brew.sh/]. Before installation, you should make sure that no other programs such as Apache or Nginx are binding to your local machine's port 80.

- `brew update`で最新バージョンの[Homebrew](https://brew.sh/)をインストール、もしくはアップデートしてください。 - Homebrewを使い、`brew install php`でPHP7.3をインストールしてください。 - [Composer](https://getcomposer.org)をインストールしてください。 - `composer global require laravel/valet`でValetをインストールしてください。`~/.composer/vendor/bin`ディレクトリが実行パスに含まれていることを確認してください。 - `valet install`コマンドを実行してください。これによりValetとDnsMasqがインストール/設定され、システム起動時に起動されるValetのデーモンが登録されます。

Valetがインストールできたら、ping foobar.testのようなコマンドで、ターミナルから*.testドメインに対してpingを実行してください。Valetが正しくインストールされていれば、このドメインは127.0.0.1へ対応していることが分かるでしょう。Once Valet is installed, try pinging any *.test domain on your terminal using a command such as ping foobar.test. If Valet is installed correctly you should see this domain responding on 127.0.0.1.

Valetはマシンが起動されると、毎回デーモンを自動的に起動します。Valetが完全にインストールされていれば、valet startvalet installを再び実行する必要は永久にありません。Valet will automatically start its daemon each time your machine boots. There is no need to run valet start or valet install ever again once the initial Valet installation is complete.

他のドメインの使用Using Another Domain

デフォルトでValetは.test TLDをプロジェクトのドメインとして処理します。他のドメインを使いたい場合、valet tld tld-nameコマンドを使ってください。By default, Valet serves your projects using the .test TLD. If you'd like to use another domain, you can do so using the valet tld tld-name command.

たとえば、.testの代わりに.appを使用したければ、valet tld appと実行します。Valetは*.appをプロジェクトのために自動的に使い始めます。For example, if you'd like to use .app instead of .test, run valet tld app and Valet will start serving your projects at *.app automatically.

データベースDatabase

データベースを使いたい場合、コマンドラインでbrew install mysql@5.7を実行し、MySQLを試してください。MySQLがインストールできたら、brew services start mysql@5.7コマンドを使い、起動します。127.0.0.1でデータベースに接続し、ユーザー名はroot、パスワードは空文字列です。If you need a database, try MySQL by running brew install mysql@5.7 on your command line. Once MySQL has been installed, you may start it using the brew services start mysql@5.7 command. You can then connect to the database at 127.0.0.1 using the root username and an empty string for the password.

PHPバージョンPHP Versions

Valetではvalet use php@versionコマンドにより、PHPバージョンを変更できます。指定されたPHPバージョンがインストールされていない場合、ValetはBrewによりインストールします。Valet allows you to switch PHP versions using the valet use php@version command. Valet will install the specified PHP version via Brew if it is not already installed:

valet use php@7.2

valet use php

アップグレードUpgrading

Valetインストールをアップデートするには、ターミナルでcomposer global updateコマンドを実行します。アップグレードできたら、valet installコマンドを実行し、必要な設定ファイルの追加アップグレードを行うのは、グッドプラクティスです。You may update your Valet installation using the composer global update command in your terminal. After upgrading, it is good practice to run the valet install command so Valet can make additional upgrades to your configuration files if necessary.

Valet2.0へのアップグレードUpgrading To Valet 2.0

Valet 2.0では、Valetの裏で動作するWebサーバをCaddyからNginxへ移動しました。このバージョン並行するは、以下のコマンドを実行し、既存のCaddyデーモンを停止し、アンインストールしてください。Valet 2.0 transitions Valet's underlying web server from Caddy to Nginx. Before upgrading to this version you should run the following commands to stop and uninstall the existing Caddy daemon:

valet stop
valet uninstall

次に、Valetの最新バージョンへアップグレードします。Valetをどのようにインストールしたかにより、通常GitかComposerを使用して行います。ValetをComposerによりインストールしている場合は、最新のメジャーバージョンにアップデートするために、以下のコマンドを実行してください。Next, you should upgrade to the latest version of Valet. Depending on how you installed Valet, this is typically done through Git or Composer. If you installed Valet via Composer, you should use the following command to update to the latest major version:

composer global require laravel/valet

真新しいValetのソースコードがダウンロードできたら、installコマンドを実行します。Once the fresh Valet source code has been downloaded, you should run the install command:

valet install
valet restart

アップグレードできたら、parklinkをやり直してください。After upgrading, it may be necessary to re-park or re-link your sites.

サイト動作Serving Sites

Valetがインストールできたら、サイトを動作させる準備ができました。Laravelサイトを動作させるために役立つ、parklinkの2コマンドを用意しています。Once Valet is installed, you're ready to start serving sites. Valet provides two commands to help you serve your Laravel sites: park and link.

parkコマンド

- `mkdir ~/Sites`のように、Mac上に新しいディレクトリを作成ししてください。次に`cd ~/Sites`し、`valet park`を実行します。このコマンドはカレントワーキングディレクトリをValetがサイトを探す親パスとして登録します。 - 次に、このディレクトリ内で、新しいLaravelサイトを作成します。`laravel new blog` - `http://blog.test`をブラウザで開きます。

必要なのはこれだけです。 これで"parked"ディレクトリ内で作成されたLaravelプロジェクトは、http://フォルダ名.test規約に従い、自動的に動作します。That's all there is to it. Now, any Laravel project you create within your "parked" directory will automatically be served using the http://folder-name.test convention.

linkコマンド

linkコマンドはparkのように親ディレクトリを指定するのではなく、各ディレクトリ中で一つのサイトを動作させるのに便利です。The link command may also be used to serve your Laravel sites. This command is useful if you want to serve a single site in a directory and not the entire directory.

- ターミナルでプロジェクトのディレクトリへ移動し、`valet link アプリケーション名`を実行します。Valetはカレントワーキングディレクトリから`~/.config/valet/Sites`内へシンボリックリンクを張ります。 - `link`コマンド実行後、ブラウザで`http://アプリケーション名.test`にアクセスできます。

リンクされた全ディレクトリをリストするには、valet linksコマンドを実行してください。シンボリックリンクを外すときは、valet unlink app-nameを使います。To see a listing of all of your linked directories, run the valet links command. You may use valet unlink app-name to destroy the symbolic link.

lightbulb">Tip!! 複数の(サブ)ドメインで同じプロジェクトを動かすために、valet linkを使用できます。サブドメインや他のドメインをプロジェクトに追加するためには、プロジェクトフォルダからvalet link subdomain.app-nameを実行します。{tip} You can use valet link to serve the same project from multiple (sub)domains. To add a subdomain or another domain to your project run valet link subdomain.app-name from the project folder.

TLSによる安全なサイト

Valetはデフォルトで通常のHTTP通信で接続します。しかし、HTTP/2を使った暗号化されたTLSで通信したい場合は、secureコマンドを使ってください。たとえば、laravel.testドメインでValetによりサイトが動作している場合、以下のコマンドを実行することで安全な通信を行います。By default, Valet serves sites over plain HTTP. However, if you would like to serve a site over encrypted TLS using HTTP/2, use the secure command. For example, if your site is being served by Valet on the laravel.test domain, you should run the following command to secure it:

valet secure laravel

サイトを「安全でない」状態へ戻し、通常のHTTP通信を使いたい場合は、unsecureコマンドです。secureコマンドと同様に、セキュアな通信を辞めたいホスト名を指定します。To "unsecure" a site and revert back to serving its traffic over plain HTTP, use the unsecure command. Like the secure command, this command accepts the host name that you wish to unsecure:

valet unsecure laravel

サイトの共有Sharing Sites

Valetはローカルサイトを世界と共有するコマンドも用意しています。Valetがインストールしてあれば、他のソフトウェアは必要ありません。Valet even includes a command to share your local sites with the world. No additional software installation is required once Valet is installed.

サイトを共有するには、ターミナルでサイトのディレクトリに移動し、valet shareコマンドを実行します。公開用のURLはクリップボードにコピーされますので、ブラウザに直接ペーストしてください。これだけです。To share a site, navigate to the site's directory in your terminal and run the valet share command. A publicly accessible URL will be inserted into your clipboard and is ready to paste directly into your browser. That's it.

サイトの共有を停止するには、Control Cでプロセスをキャンセルしてください。To stop sharing your site, hit Control C to cancel the process.

カスタムValetドライバCustom Valet Drivers

Valetでサポートされていない、他のフレームワークやCMSでPHPアプリケーションを実行するには、独自のValet「ドライバ」を書く必要があります。Valetをインストールすると作成される、~/.config/valet/DriversディレクトリにSampleValetDriver.phpファイルが存在しています。このファイルは、カスタムドライバーをどのように書いたら良いかをデモンストレートするサンプルドライバの実装コードです。ドライバを書くために必要なservesisStaticFilefrontControllerPathの3メソッドを実装するだけです。You can write your own Valet "driver" to serve PHP applications running on another framework or CMS that is not natively supported by Valet. When you install Valet, a ~/.config/valet/Drivers directory is created which contains a SampleValetDriver.php file. This file contains a sample driver implementation to demonstrate how to write a custom driver. Writing a driver only requires you to implement three methods: serves, isStaticFile, and frontControllerPath.

全3メソッドは$sitePath$siteName$uriを引数で受け取ります。$sitePathは、/Users/Lisa/Sites/my-projectのように、サイトプロジェクトへのフルパスです。$siteNameは"ホスト" / "サイト名"記法のドメイン(my-project)です。$uriはやって来たリクエストのURI(/foo/bar)です。All three methods receive the $sitePath, $siteName, and $uri values as their arguments. The $sitePath is the fully qualified path to the site being served on your machine, such as /Users/Lisa/Sites/my-project. The $siteName is the "host" / "site name" portion of the domain (my-project). The $uri is the incoming request URI (/foo/bar).

カスタムValetドライバを書き上げたら、フレームワークValetDriver.php命名規則をつかい、~/.config/valet/Driversディレクトリ下に設置してください。たとえば、WordPress用にカスタムValetドライバを書いたら、ファイル名はWordPressValetDriver.phpになります。Once you have completed your custom Valet driver, place it in the ~/.config/valet/Drivers directory using the FrameworkValetDriver.php naming convention. For example, if you are writing a custom valet driver for WordPress, your file name should be WordPressValetDriver.php.

カスタムValetドライバで実装する各メソッドのサンプルコードを見ていきましょう。Let's take a look at a sample implementation of each method your custom Valet driver should implement.

servesメソッドThe serves Method

servesメソッドは、そのドライバがやって来たリクエストを処理すべき場合に、trueを返してください。それ以外の場合はfalseを返してください。そのためには、メソッドの中で、渡された$sitePathの内容が、動作させようとするプロジェクトタイプを含んでいるかを判定します。The serves method should return true if your driver should handle the incoming request. Otherwise, the method should return false. So, within this method you should attempt to determine if the given $sitePath contains a project of the type you are trying to serve.

では擬似サンプルとして、WordPressValetDriverを書いてみましょう。servesメソッドは以下のようになります。For example, let's pretend we are writing a WordPressValetDriver. Our serves method might look something like this:

/**
 * このドライバでリクエストを処理するか決める
 *
 * @param  string  $sitePath
 * @param  string  $siteName
 * @param  string  $uri
 * @return bool
 */
public function serves($sitePath, $siteName, $uri)
{
    return is_dir($sitePath.'/wp-admin');
}

isStaticFileメソッドThe isStaticFile Method

isStaticFileはリクエストが画像やスタイルシートのような「静的」なファイルであるかを判定します。ファイルが静的なものであれば、そのファイルが存在するディスク上のフルパスを返します。リクエストが静的ファイルでない場合は、falseを返します。The isStaticFile should determine if the incoming request is for a file that is "static", such as an image or a stylesheet. If the file is static, the method should return the fully qualified path to the static file on disk. If the incoming request is not for a static file, the method should return false:

/**
 * リクエストが静的なファイルであるかを判定する
 *
 * @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: note isStaticFileメソッドは、リクエストのURIが/ではなく、servesメソッドでtrueが返された場合のみ呼びだされます。{note} The isStaticFile method will only be called if the serves method returns true for the incoming request and the request URI is not /.

frontControllerPathメソッドThe frontControllerPath Method

frontControllerPathメソッドは、アプリケーションの「フロントコントローラ」への絶対パスを返します。通常は"index.php`ファイルか、似たようなファイルでしょう。The frontControllerPath method should return the fully qualified path to your application's "front controller", which is typically your "index.php" file or equivalent:

/**
 * アプリケーションのフロントコントローラへの絶対パスの取得
 *
 * @param  string  $sitePath
 * @param  string  $siteName
 * @param  string  $uri
 * @return string
 */
public function frontControllerPath($sitePath, $siteName, $uri)
{
    return $sitePath.'/public/index.php';
}

ローカルドライバLocal Drivers

一つのアプリケーションに対して、Valetのカスタムドライバを定義する場合は、アプリケーションのルートディレクトリにLocalValetDriver.phpを作成してください。カスタムドライバは、ベースのValetDriverクラスか、LaravelValetDriverのような、既存のアプリケーション専用のドライバを拡張します。If you would like to define a custom Valet driver for a single application, create a LocalValetDriver.php in the application's root directory. Your custom driver may extend the base ValetDriver class or extend an existing application specific driver such as the LaravelValetDriver:

class LocalValetDriver extends LaravelValetDriver
{
    /**
     * リクエストに対し、このドライバを動作させるかを決める
     *
     * @param  string  $sitePath
     * @param  string  $siteName
     * @param  string  $uri
     * @return bool
     */
    public function serves($sitePath, $siteName, $uri)
    {
        return true;
    }

    /**
     * アプリケーションのフロントコントローラに対する完全な解決済みパスを取得する
     *
     * @param  string  $sitePath
     * @param  string  $siteName
     * @param  string  $uri
     * @return string
     */
    public function frontControllerPath($sitePath, $siteName, $uri)
    {
        return $sitePath.'/public_html/index.php';
    }
}

その他のValetコマンドOther Valet Commands

コマンドCommand 説明Description
valet forgetvalet forget "park"された(サイト検索の親ディレクトリとして登録されたJ)ディレクトリでこのコマンドを実行し、サイト検索対象のディレクトリリストから外します。Run this command from a "parked" directory to remove it from the parked directory list.
valet pathsvalet paths "park"されたすべてのパスを表示します。View all of your "parked" paths.
valet restartvalet restart Valetデーモンをリスタートします。Restart the Valet daemon.
valet startvalet start Valetデーモンをスタートします。Start the Valet daemon.
valet stopvalet stop Valetデーモンを停止します。Stop the Valet daemon.
valet uninstallvalet uninstall Valetデーモンを完全にアンインストールします。Uninstall the Valet daemon entirely.

章選択

設定

明暗テーマ
light_mode
dark_mode
brightness_auto システム設定に合わせる
テーマ選択
photo_size_select_actual デフォルト
photo_size_select_actual モノクローム(白黒)
photo_size_select_actual Solarized風
photo_size_select_actual GitHub風(青ベース)
photo_size_select_actual Viva(黄緑ベース)
photo_size_select_actual Happy(紫ベース)
photo_size_select_actual Mint(緑ベース)
コードハイライトテーマ選択

明暗テーマごとに、コードハイライトのテーマを指定できます。

テーマ配色確認
スクリーン表示幅
640px
80%
90%
100%

768px以上の幅があるときのドキュメント部分表示幅です。

インデント
無し
1rem
2rem
3rem
原文確認
原文を全行表示
原文を一行ずつ表示
使用しない

※ 段落末のEボタンへカーソルオンで原文をPopupします。

Diff表示形式
色分けのみで区別
行頭の±で区別
削除線と追記で区別

※ [tl!…]形式の挿入削除行の表示形式です。

Pagination和文
ペジネーション
ペギネーション
ページネーション
ページ付け
Scaffold和文
スカフォールド
スキャフォールド
型枠生成
本文フォント

総称名以外はCSSと同様に、"〜"でエスケープしてください。

コードフォント

総称名以外はCSSと同様に、"〜"でエスケープしてください。

保存内容リセット

localStrageに保存してある設定項目をすべて削除し、デフォルト状態へ戻します。

ヘッダー項目移動

キーボード操作