インストールInstallation
サーバー要件Server Requirements
Laravelフレームワークを動作させるには多少のシステム要件があります。もちろん、Laravel Homestead仮想マシンでは、要求が全て満たされています。The Laravel framework has a few system requirements. Of course, all of these requirements are satisfied by the Laravel Homestead[/docs/{{version}}/homestead] virtual machine:
LaravelのインストールInstalling Laravel
LaravelはComposerを依存パッケージの管理に使用しています。ですから、Laravelを始める前に、自分の開発機にComposerを確実にインストールしておいてください。Laravel utilizes Composer[http://getcomposer.org] to manage its dependencies. So, before using Laravel, make sure you have Composer installed on your machine.
LaravelインストーラーVia Laravel Installer
最初にComposerを使用し、Laravelインストーラーをダウンロードします。First, download the Laravel installer using Composer:
composer global require "laravel/installer"
laravel
コマンドが端末で実行できるように、~/.composer/vendor/bin
ディレクトリーへ実行PATHを通してください。Make sure to place the
~/.composer/vendor/bin
directory in
your PATH so the laravel
executable can
be located by your system.
インストールが完了したら、あとはlaravel
new
コマンドを実行するだけで、指定したディレクトリーに真新しいLaravelプロジェクトが作成されます。例えば、laravel
new
blog
と打ち込めば、blog
というディレクトリーに、必要な依存パッケージと一緒に、Laravelがインストールされます。このインストール方法はComposerを使うよりも多少早いです。Once installed, the simple
laravel new
command will create a fresh
Laravel installation in the directory you specify.
For instance, laravel new blog
will
create a directory named blog
containing a fresh Laravel installation with all of
Laravel's dependencies already installed. This
method of installation is much faster than
installing via Composer:
laravel new blog
Composer Create-ProjectVia Composer Create-Project
もしくは、ターミナルでComposerのcreate-project
コマンドを実行して、Laravelをインストールすることもできます。Alternatively, you may also
install Laravel by issuing the Composer
create-project
command in your
terminal:
composer create-project laravel/laravel blog "5.1.*"
設定Configuration
基本設定Basic Configuration
フレームワークで使用する設定ファイルは全て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.
ディレクトリーパーミッションDirectory Permissions
Laravelをインストールした後に多少のパーミッションの設定が必要です。storage
下とbootstrap/cache
ディレクトリーをWebサーバーから書き込み可能にしてください。Homestead仮想マシンを使用する場合は、あらかじめ設定されています。After installing Laravel, you may
need to configure some permissions. Directories
within the storage
and the
bootstrap/cache
directories should be
writable by your web server. If you are using the
Homestead[/docs/{{version}}/homestead]
virtual machine, these permissions should already be
set.
アプリケーションキーApplication Key
次にインストール後に行うべきなのは、アプリケーションキーにランダムな文字列を設定することです。ComposerかLaravelインストーラーを使ってインストールしていれば、key:generate
コマンドにより既に設定済みです。通常、この文字列は32文字にすべきです。キーは.env
環境ファイルに設定されます。もし、.env.example
ファイルをまだ.env
にリネームしていなければ、今すぐ行ってください。アプリケーションキーが設定されていなければ、ユーザーセッションや他の暗号化済みデーターは安全ではありません!The next thing you should do
after installing Laravel is set your application key
to a random string. If you installed Laravel via
Composer or the Laravel installer, this key has
already been set for you by the
key:generate
command. Typically, this
string should be 32 characters long. The key can be
set in the .env
environment file. If
you have not renamed the .env.example
file to .env
, you should do that now.
If the application key is not set, your user
sessions and other encrypted data will not be
secure!
その他の設定Additional Configuration
Laravelのその他の設定は、最初に指定する必要がありません。すぐに開発を開始しても大丈夫です!
しかし、config/app.php
ファイルと、その中の記述を確認しておいたほうが良いでしょう。アプリケーションに合わせ変更したい、timezone
やlocal
のような多くのオプションが含まれています。Laravel needs almost no other
configuration out of the box. You are free to get
started developing! However, you may wish to review
the config/app.php
file and its
documentation. It contains several options such as
timezone
and locale
that
you may wish to change according to your
application.
以下のようなLaravelのコンポーネントについても、設定しておいたほうが良いでしょう。You may also want to configure a few additional components of Laravel, such as:
- キャッシュCache[/docs/{{version}}/cache#configuration]
- データベースDatabase[/docs/{{version}}/database#configuration]
- セッションSession[/docs/{{version}}/session#configuration]
Laravelをインストールしたら、ローカル動作環境の設定もしておきましょう。Once Laravel is installed, you should also configure your local environment[/docs/{{version}}/installation#environment-configuration].
きれいなURLPretty URLs
ApacheApache
index.php
をURLへ付けなくても利用できるようにするため、public/.htaccess
ファイルが最初からフレームワークへ含まれています。Apacheを利用し、Laravelアプリケーションを動かす場合、mod_rewrite
モジュールを確実に有効にしてください。The framework ships
with a public/.htaccess
file that is used to allow URLs without
index.php
. If you use
Apache to serve your Laravel
application, be sure to enable the
mod_rewrite
module.
Laravelに付属している、.htaccess
ファイルが、皆さんのApache環境で動作しない場合は、以下の設定を試してください。If the
.htaccess
file that ships
with Laravel does not work with your
Apache installation, try this
one:
Options FollowSymLinks
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
NginxNginx
Nginxでは、以下の仮想ホストの設定により、「きれいな」URLが使えるようになります。On Nginx, the following directive in your site configuration will allow "pretty" URLs:
location / {
try_files $uri $uri/ /index.php?$query_string;
}
もちろん、Homesteadを使う場合は、きれいなURLが自動的に設定されます。Of course, when using Homestead[/docs/{{version}}/homestead], pretty URLs will be configured automatically.
動作環境設定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を活用しているので朝飯前です。インストールしたての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. You may use the
env
helper to retrieve
values from these variables. In fact, if
you review the Laravel configuration
files, you will notice several of the
options already using this
helper!
ローカルサーバーの要求に合わせ、環境変数を変更してください。実働環境でも同様です。しかし、.env
ファイルをアプリケーションのソース管理下へ追加してはいけません。アプリケーションを利用する各開発者/サーバーが、別々の環境値を設定できるからです。Feel free to modify
your environment variables as needed for
your own local server, as well as your
production environment. However, 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
ファイルを含めておいたほうが良いでしょう。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.
現在の動作環境へのアクセスAccessing The Current Application 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
メソッドの引数を渡せば、指定した値に動作環境が一致するか確認できます。必要であれば、複数の環境を指定できます。You may also pass
arguments to the
environment
method to check
if the environment matches a given
value. You may even pass multiple values
if necessary:
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 can
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.
設定値へのアクセスAccessing Configuration Values
設定値にはグローバルconfig
ヘルパ関数を使い、簡単にアクセスできます。設定値には「ドット」記法でアクセスします。ファイル名に続けてアクセスしたいオプションです。設定オプションが存在しない場合、デフォルト値が返されます。You may easily access
your configuration values using the
global config
helper
function. 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']);
アプリケーションの命名Naming Your Application
Laravelをインストールすると、アプリケーションに「名前」を付けたくなります。app
ディレクトリーはデフォルトでApp
名前空間下にあり、PSR-4オートローディング規約へ従い、Composerによりオートローディングされます。しかし、app:name
Artisanコマンドにより、簡単にアプリケーションに適した名前空間へ変更できます。After installing
Laravel, you may wish to
"name" your application. By
default, the app
directory
is namespaced under App
,
and autoloaded by Composer using the
PSR-4 autoloading
standard[http://www.php-fig.org/psr/psr-4/].
However, you may change the namespace to
match the name of your application,
which you can easily do via the
app:name
Artisan
command.
例えばアプリケーションを"Horsefly"と名付けたければ、インストールルートのディレクトリーで、次のコマンドを実行してください。For example, if your application is named "Horsefly", you could run the following command from the root of your installation:
php artisan app:name Horsefly
アプリケーションの名前付けは強制ではありません。App
名前空間のままでもかまいません。Renaming your
application is entirely optional, and
you are free to keep the
App
namespace if you
wish.
メンテナンスモード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
.
メンテナンスモードとキュー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.