イントロダクションIntroduction
Laravelフレームワークの前設定ファイルは、config
ディレクトリに保存されています。各オプションには詳しいコメントが付いているので、各ファイルを一読し、使用できるオプションを把握しておきましょう。All of the configuration files
for the Laravel framework are stored in the
config
directory. Each option is
documented, so feel free to look through the files
and get familiar with the options available to
you.
設定値へのアクセスAccessing Configuration Values
アプリケーションのどこからでも、設定値へはグローバルconfig
ヘルパ関数を使用し、簡単にアクセスできます。設定値にはアクセスしたい値を含んだファイル名とオプション名で構成された、「ドット」記法でアクセスできます。デフォルト値も指定でき、設定オプションが存在しない場合に返されます。You may easily access your
configuration values using the global
config
helper function from anywhere in
your application. The configuration values may be
accessed using "dot" syntax, which
includes the name of the file and option you wish to
access. A default value may also be specified and
will be returned if the configuration option does
not exist:
$value = config('app.timezone');
実行時に設定値を指定したい場合、config
ヘルパに配列を渡します。To set configuration values at
runtime, pass an array to the config
helper:
config(['app.timezone' => 'America/Chicago']);
環境設定Environment Configuration
アプリケーションを実行している環境にもとづき、別の設定値に切り替えられると便利です。たとえば、ローカルと実働サーバでは、異なったキャッシュドライバを使いたいと思うでしょう。環境ベースの設定は簡単に利用できます。It is often helpful to have different configuration values based on the environment the application is running in. For example, you may wish to use a different cache driver locally than you do on your production server. It's easy using environment based configuration.
これを簡単にできるようにするために、LaravelではVance Lucas氏により作成された、DotEnv
PHPライブラリーを使用しています。新たにLaravelをインストールすると、アプリケーションのルートディレクトリには、env.example
ファイルが含まれています。ComposerによりLaravelをインストールした場合は自動的に、このファイルは.env
に名前が変更されます。Composerを使わずにインストールした場合は、名前を変更してください。To make this a cinch, Laravel
utilizes the
DotEnv[https://github.com/vlucas/phpdotenv]
PHP library by Vance Lucas. In a fresh Laravel
installation, the root directory of your application
will contain a .env.example
file. If
you install Laravel via Composer, this file will
automatically be renamed to .env
.
Otherwise, you should rename the file
manually.
このファイルにリストしている値は、アプリケーションがリクエストを受け取った時点で、$_ENV
PHPスーパーグローバル変数へロードされます。しかしながら、設定ファイルの変数をenv
ヘルパを使用して、値を取得できます。実際にLaravelの設定ファイルを見てもらえば、このヘルパで多くのオプションが使われているのに気がつくでしょう。All of the variables listed in
this file will be loaded into the $_ENV
PHP super-global when your application receives a
request. However, you may use the env
helper to retrieve values from these variables in
your configuration files. In fact, if you review the
Laravel configuration files, you will notice several
of the options already using this helper:
'debug' => env('APP_DEBUG', false),
env
関数の第2引数は「デフォルト値」です。この値は指定したキーの環境変数が存在しない場合に返されます。The second value passed to the
env
function is the "default
value". This value will be used if no
environment variable exists for the given
key.
.env
ファイルをアプリケーションのソース管理に含めてはいけません。アプリケーションの各開発者/サーバで異なった環境設定が必要になるからです。Your .env
file
should not be committed to your application's source
control, since each developer / server using your
application could require a different environment
configuration.
チーム開発を行っている場合、.env.example
ファイルをアプリケーションに含めたいと思うでしょう。サンプルの設定ファイルに、プレースホルダーとして値を設定しておけば、チームの他の開発者は、アプリケーションを実行するために必要な環境変数をはっきりと理解できるでしょう。If you are developing with a
team, you may wish to continue including a
.env.example
file with your
application. By putting place-holder values in the
example configuration file, other developers on your
team can clearly see which environment variables are
needed to run your application.
現在環境の決定Determining The Current Environment
現在のアプリケーション環境は、.env
ファイルのAPP_ENV
変数により決まります。APP
ファサードのenvironment
メソッドにより、この値へアクセスできます。The current application
environment is determined via the
APP_ENV
variable from your
.env
file. You may access this value
via the environment
method on the
App
facade[/docs/{{version}}/facades]:
$environment = App::environment();
指定した値と一致する環境であるかを確認するために、environment
メソッドへ引数を渡すこともできます。必要であれば、複数の値をenvironment
メソッドへ渡せます。値のどれかと一致すれば、メソッドはtrue
を返します。You may also pass arguments to
the environment
method to check if the
environment matches a given value. If necessary, you
may even pass multiple values to the
environment
method. If the environment
matches any of the given values, the method will
return true
:
if (App::environment('local')) {
// 環境はlocal
}
if (App::environment('local', 'staging')) {
// 環境はlocalかstaging
}
app
ヘルパメソッドを使い、アプリケーションインスタンスからでもアクセスできます。An application instance may also
be accessed via the app
helper
method:
$environment = app()->environment();
設定キャッシュConfiguration Caching
アプリケーションをスピードアップさせるために、全設定ファイルを一つのファイルへまとめる、config:cache
Artisanコマンドを使ってください。これによりアプリケーションの全設定ファイルのオプションが、単一のファイルに結合され、素早くロードできるようになります。To give your application a speed
boost, you should cache all of your configuration
files into a single file using the
config:cache
Artisan command. This will
combine all of the configuration options for your
application into a single file which will be loaded
quickly by the framework.
通常は開発ルーティンの一環として、php artisan
config:cache
コマンドを実行すべきでしょう。アプリケーション開発時は、しばしば設定が変更されるでしょうから、ローカルでの開発中にこのコマンドを実行してはいけません。You should typically run the
php artisan config:cache
command as
part of your production deployment routine. The
command should not be run during local development
as configuration options will frequently need to be
changed during the course of your application's
development.
メンテナンスモードMaintenance Mode
アプリケーションをメンテナンスモードにすると、アプリケーションに対するリクエストにはすべてカスタムビューが表示されるようになります。アプリケーションのアップデート中や、メンテナンス中に、アプリケーションを簡単に「無効」状態にできます。メンテナンスモードのチェックは、アプリケーションのデフォルトミドルウェアスタックに含まれています。アプリケーションがメンテナンスモードの時、ステータスコード503のHttpException
例外が投げられます。When your application is in
maintenance mode, a custom view will be displayed
for all requests into your application. This makes
it easy to "disable" your application
while it is updating or when you are performing
maintenance. A maintenance mode check is included in
the default middleware stack for your application.
If the application is in maintenance mode, an
HttpException
will be thrown with a
status code of 503.
メンテナンスモードにするには、down
Artisanコマンドを実行するだけです。To enable
maintenance mode, simply execute the
down
Artisan command:
php artisan down
メンテナンスモードから抜けるには、up
コマンドを使います。To disable maintenance mode, use
the up
command:
php artisan up
メンテナンスモードのレスポンステンプレートMaintenance Mode Response Template
メンテナンスモードレスポンスのデフォルトテンプレートは、resources/views/errors/503.blade.php
です。アプリケーションの必要に合わせ、自由に変更してください。The default template for
maintenance mode responses is located in
resources/views/errors/503.blade.php
.
You are free to modify this view as needed for your
application.
メンテナンスモードとキューMaintenance Mode & Queues
アプリケーションがメンテナンスモードの間、キューされたジョブは実行されません。メンテナンスモードから抜け、アプリケーションが通常状態へ戻った時点で、ジョブは続けて処理されます。While your application is in maintenance mode, no queued jobs[/docs/{{version}}/queues] will be handled. The jobs will continue to be handled as normal once the application is out of maintenance mode.
メンテナンスモードの代替Alternatives To Maintenance Mode
メンテナンスモードでは、アプリケーションがその間ダウンタイムになってしまいますので、Laravelでの開発でゼロダウンタイムを実現するEnvoyerのような代替サービスを検討してください。Since maintenance mode requires your application to have several seconds of downtime, you may consider alternatives like Envoyer[https://envoyer.io] to accomplish zero-downtime deployment with Laravel.