イントロダクションIntroduction
Laravelのデフォルトアプリケーション構造はアプリケーションの大小にかかわらず、素晴らしいスタートを切ってもらえることを意図しています。アプリケーションは皆さんのお好みに応じ、自由に体系立ててください。クラスがComposerによりオートローディングできるならば、Laravelはクラスをどこに配置するか強制することはまずありません。The default Laravel application structure is intended to provide a great starting point for both large and small applications. But you are free to organize your application however you like. Laravel imposes almost no restrictions on where any given class is located - as long as Composer can autoload the class.
Laravel Bootcampでは、Laravelのフレームワークを実際に体験しながら、Laravelアプリケーションを構築することができます。NoteNew to Laravel? Check out the Laravel Bootcamp[https://bootcamp.laravel.com] for a hands-on tour of the framework while we walk you through building your first Laravel application.
Note:Laravelは初めてですか?
プロジェクトディレクトリThe Root Directory
appディレクトリThe App Directory
app
ディレクトリは、アプリケーションのコアコードを配置します。このフォルダの詳細は、この後に説明します。しかし、アプリケーションのほとんど全部のクラスは、このディレクトリの中に設定されることを覚えておいてください。The app
directory
contains the core code of your application. We'll
explore this directory in more detail soon; however,
almost all of the classes in your application will
be in this directory.
bootstrapディレクトリThe Bootstrap Directory
bootstrap
ディレクトリには、フレームワークを初期起動処理するapp.php
ファイルを設置しています。このディレクトリには、ルートやサービスのキャッシュファイルなど、パフォーマンスを最適化するためのフレームワークで生成されたファイルを含むcache
ディレクトリも含んでいます。通常、このディレクトリ内のファイルを変更する必要はありません。The bootstrap
directory contains the app.php
file
which bootstraps the framework. This directory also
houses a cache
directory which contains
framework generated files for performance
optimization such as the route and services cache
files. You should not typically need to modify any
files within this directory.
configディレクトリThe Config Directory
config
ディレクトリは名前が示す通り、アプリケーションの全設定ファイルを設置しています。全ファイルに目を通し、設定可能なオプションに慣れ親しんでおくのは良い考えでしょう。The config
directory, as the name implies, contains all of your
application's configuration files. It's a great idea
to read through all of these files and familiarize
yourself with all of the options available to
you.
databaseディレクトリThe Database Directory
database
フォルダはデータベースのマイグレーションとモデルファクトリ、初期値設定(シーディング)を配置しています。ご希望であれば、このディレクトリをSQLiteデータベースの設置場所としても利用できます。The database
directory contains your database migrations, model
factories, and seeds. If you wish, you may also use
this directory to hold an SQLite
database.
langディレクトリThe Lang Directory
lang
ディレクトリには、アプリケーションのすべての言語ファイルを格納します。The lang
directory
houses all of your application's language
files.
publicディレクトリThe Public Directory
public
ディレクトリには、アプリケーションへの全リクエストの入り口となり、オートローディングを設定するindex.php
ファイルがあります。また、このディレクトリにはアセット(画像、JavaScript、CSSなど)を配置します。The public
directory
contains the index.php
file, which is
the entry point for all requests entering your
application and configures autoloading. This
directory also houses your assets such as images,
JavaScript, and CSS.
resourcesディレクトリThe Resources Directory
resources
ディレクトリには、ビューや、CSS、JavaScriptなどのコンパイルしていない、素のアセットを格納します。The resources
directory contains your
views[/docs/{{version}}/views] as well as
your raw, un-compiled assets such as CSS or
JavaScript.
routesディレクトリThe Routes Directory
routes
ディレクトリは、アプリケーションのすべてのルート定義を配置しています。デフォルトでルートファイルをいくつかLaravelは用意しています。web.php
、api.php
、console.php
、channels.php
。The routes
directory
contains all of the route definitions for your
application. By default, several route files are
included with Laravel: web.php
,
api.php
, console.php
, and
channels.php
.
web.php
ファイルは、RouteServiceProvider
がweb
ミドルウェアグループへ配置するルートを記述します。これにより、セッション状態、CSRF保護、およびクッキー暗号化が提供されます。アプリケーションがステートレスのRESTful
APIを提供しない場合は、すべてのルートがweb.php
ファイルでほぼ定義されるでしょう。The web.php
file
contains routes that the
RouteServiceProvider
places in the
web
middleware group, which provides
session state, CSRF protection, and cookie
encryption. If your application does not offer a
stateless, RESTful API then all your routes will
most likely be defined in the web.php
file.
api.php
ファイルは、RouteServiceProvider
がapi
ミドルウェアグループへ配置するルートを記述します。これらのルートはステートレスであることが意図されているため、これらのルートを介してアプリケーションに入るリクエストは、トークンを介してで認証されることを意図しており、セッション状態にアクセスできません。The api.php
file
contains routes that the
RouteServiceProvider
places in the
api
middleware group. These routes are
intended to be stateless, so requests entering the
application through these routes are intended to be
authenticated via
tokens[/docs/{{version}}/sanctum] and will
not have access to session state.
console.php
ファイルは、クロージャベースのコンソールコマンドをすべて定義する場所です。各クロージャはコマンドインスタンスと結合されるため、各コマンドのIOメソッドを操作する簡単なアプローチが可能です。このファイルはHTTPルートを定義しませんが、アプリケーションへのコンソールベースのエントリポイント(ルート)を定義しています。The console.php
file
is where you may define all of your closure based
console commands. Each closure is bound to a command
instance allowing a simple approach to interacting
with each command's IO methods. Even though this
file does not define HTTP routes, it defines console
based entry points (routes) into your
application.
channels.php
ファイルは、アプリケーションがサポートするすべてのイベントブロードキャストチャンネルを登録できる場所です。The channels.php
file is where you may register all of the event
broadcasting[/docs/{{version}}/broadcasting]
channels that your application supports.
storageディレクトリThe Storage Directory
storage
ディレクトリには、ログ、コンパイル済みBladeテンプレート、ファイルベースのセッション、ファイルキャッシュ、およびフレームワークが作成したその他のファイルが含まれます。このディレクトリは、app
、framework
、logs
ディレクトリに分離されています。app
ディレクトリは、アプリケーションが作成したファイルを保存するために使用できます。framework
ディレクトリは、フレームワークが作成したファイルとキャッシュを保存するために使用します。最後に、logs
ディレクトリにはアプリケーションのログファイルを保存しています。The storage
directory contains your logs, compiled Blade
templates, file based sessions, file caches, and
other files generated by the framework. This
directory is segregated into app
,
framework
, and logs
directories. The app
directory may be
used to store any files generated by your
application. The framework
directory is
used to store framework generated files and caches.
Finally, the logs
directory contains
your application's log files.
storage/app/public
ディレクトリは、プロファイルアバターなど、一般にアクセス可能である必要のあるユーザー生成ファイルを保存するために使用します。このディレクトリを指すシンボリックリンクをpublic/storage
に作成する必要があります。php
artisan storage:link
Artisanコマンドを使用してリンクを作成できます。The
storage/app/public
directory may be
used to store user-generated files, such as profile
avatars, that should be publicly accessible. You
should create a symbolic link at
public/storage
which points to this
directory. You may create the link using the
php artisan storage:link
Artisan
command.
testsディレクトリThe Tests Directory
tests
ディレクトリには自動テストを設置します。たとえば、PHPUnitユニットテストと機能テストは、はじめから提供されています。各テストクラスには、Test
という接尾辞を付ける必要があります。phpunit
またはphpvendor/bin/phpunit
コマンドを使用してテストを実行できます。または、テスト結果をより詳細で美しい表現にしたい場合は、php
artisan test
Artisanコマンドを使用してテストを実行してください。The tests
directory
contains your automated tests. Example
PHPUnit[https://phpunit.de/] unit tests and
feature tests are provided out of the box. Each test
class should be suffixed with the word
Test
. You may run your tests using the
phpunit
or php
vendor/bin/phpunit
commands. Or, if you
would like a more detailed and beautiful
representation of your test results, you may run
your tests using the php artisan test
Artisan command.
vendorディレクトリThe Vendor Directory
vendor
ディレクトリには、Composerによる依存パッケージが配置されます。The vendor
directory
contains your
Composer[https://getcomposer.org]
dependencies.
appディレクトリThe App Directory
アプリケーションの主要な部分は、app
ディレクトリ内に配置します。このディレクトリはデフォルトで、App
名前空間のもとに置かれており、PSR-4オートローディング規約を使い、Composerがオートロードしています。The majority of your application
is housed in the app
directory. By
default, this directory is namespaced under
App
and is autoloaded by Composer using
the PSR-4 autoloading
standard[https://www.php-fig.org/psr/psr-4/].
app
ディレクトリは多様なサブディレクトリを持っています。Console
、Http
、Providers
などです。Console
とHttp
ディレクトリは、アプリケーションの「コア」へAPIを提供していると考えてください。HTTPプロトコルとCLIは両方共にアプリケーションと相互に関係するメカニズムですが、実際のアプリケーションロジックではありません。言い換えれば、これらはアプリケーションに指示を出す、2つの方法に過ぎません。Console
ディレクトリは全Artisanコマンドで構成され、一方のHttp
ディレクトリはコントローラやフィルター、リクエストにより構成されています。The app
directory
contains a variety of additional directories such as
Console
, Http
, and
Providers
. Think of the
Console
and Http
directories as providing an API into the core of
your application. The HTTP protocol and CLI are both
mechanisms to interact with your application, but do
not actually contain application logic. In other
words, they are two ways of issuing commands to your
application. The Console
directory
contains all of your Artisan commands, while the
Http
directory contains your
controllers, middleware, and requests.
クラス生成のためのmake
Artisanコマンドを使用することで、さまざまなディレクトリがapp
ディレクトリ内に作成されます。たとえば、app/Jobs
ディレクトリは、ジョブクラスを生成するmake:job
Artisanコマンドを実行するまで存在していません。A
variety of other directories will be generated
inside the app
directory as you use the
make
Artisan commands to generate
classes. So, for example, the app/Jobs
directory will not exist until you execute the
make:job
Artisan command to generate a
job class.
Note:Artisanコマンドにより、
app
ディレクトリ下にたくさんのクラスが生成されます。使用可能なコマンドを確認するには、php artisan list make
コマンドをターミナルで実行してください。Note
Many of the classes in theapp
directory can be generated by Artisan via commands. To review the available commands, run thephp artisan list make
command in your terminal.
BroadcastingディレクトリThe Broadcasting Directory
Broadcasting
ディレクトリは、アプリケーションの全ブロードキャストチャンネルクラスで構成します。これらのクラスは、make:channel
コマンドで生成されます。このディレクトリはデフォルトでは存在しませんが、最初にチャンネルを生成したときに作成されます。チャンネルについての詳細は、イベントブロードキャストのドキュメントで確認してください。The Broadcasting
directory contains all of the broadcast channel
classes for your application. These classes are
generated using the make:channel
command. This directory does not exist by default,
but will be created for you when you create your
first channel. To learn more about channels, check
out the documentation on event
broadcasting[/docs/{{version}}/broadcasting].
ConsoleディレクトリThe Console Directory
Console
ディレクトリは、アプリケーションの全カスタムArtisanコマンドで構成します。これらのコマンドクラスはmake:command
コマンドにより生成されます。コンソールカーネルもこのディレクトリ内にあり、カスタムArtisanコマンドや、タスクのスケジュールを登録します。The Console
directory contains all of the custom Artisan
commands for your application. These commands may be
generated using the make:command
command. This directory also houses your console
kernel, which is where your custom Artisan commands
are registered and your scheduled
tasks[/docs/{{version}}/scheduling] are
defined.
EventsディレクトリThe Events Directory
このディレクトリはデフォルトで存在していません。event:generate
かmake:event
Artisanコマンド実行時に作成されます。Events
ディレクトリは、イベントクラスを設置する場所です。イベントは特定のアクションが起きたことをアプリケーションの別の部分へ知らせるために使われ、柔軟性と分離性を提供しています。This directory does not exist by
default, but will be created for you by the
event:generate
and
make:event
Artisan commands. The
Events
directory houses event
classes[/docs/{{version}}/events]. Events
may be used to alert other parts of your application
that a given action has occurred, providing a great
deal of flexibility and decoupling.
ExceptionsディレクトリThe Exceptions Directory
Exceptions
ディレクトリはアプリケーションの例外ハンドラで構成します。また、アプリケーションから投げる例外を用意するにも適した場所でしょう。例外のログやレンダ方法をカスタマイズしたい場合は、このディレクトリのHandler
クラスを修正してください。The Exceptions
directory contains your application's exception
handler and is also a good place to place any
exceptions thrown by your application. If you would
like to customize how your exceptions are logged or
rendered, you should modify the Handler
class in this directory.
HttpディレクトリThe Http Directory
Http
ディレクトリはコントローラ、ミドルウェア、フォームリクエストを設置します。アプリケーションへのリクエストを処理するロジックは、ほぼすべてこのディレクトリ内に設置します。The Http
directory
contains your controllers, middleware, and form
requests. Almost all of the logic to handle requests
entering your application will be placed in this
directory.
JobsディレクトリThe Jobs Directory
このディレクトリはデフォルトで存在していません。make:job
Artisanコマンドを実行すると作成されます。Jobs
ディレクトリはアプリケーションのキュー投入可能なジョブを置いておく場所です。Jobs
はアプリケーションによりキューに投入されるか、もしくは現在のリクエストサイクル中に同期的に実行されます。現在のリクエストサイクル中に同期的に実行するジョブは、コマンドパターンを実装しているため、時に「コマンド」と呼ばれることがあります。This directory does not exist by
default, but will be created for you if you execute
the make:job
Artisan command. The
Jobs
directory houses the queueable
jobs[/docs/{{version}}/queues] for your
application. Jobs may be queued by your application
or run synchronously within the current request
lifecycle. Jobs that run synchronously during the
current request are sometimes referred to as
"commands" since they are an
implementation of the command
pattern[https://en.wikipedia.org/wiki/Command_pattern].
ListenersディレクトリThe Listeners Directory
このディレクトリはデフォルトで存在していません。event:generate
かmake:listener
Artisanコマンドを実行すると、作成されます。Listeners
ディレクトリには、eventsイベントを処理するクラスを設置します。イベントリスナはイベントインスタンスを受け取り、発行されたイベントへ対応するロジックを実行します。たとえば、UserRegistered
(ユーザー登録)イベントは、SendWelcomeEmail
(ウェルカムメール送信)リスナにより処理されることになるでしょう。This directory does not exist by
default, but will be created for you if you execute
the event:generate
or
make:listener
Artisan commands. The
Listeners
directory contains the
classes that handle your
events[/docs/{{version}}/events]. Event
listeners receive an event instance and perform
logic in response to the event being fired. For
example, a UserRegistered
event might
be handled by a SendWelcomeEmail
listener.
MailディレクトリThe Mail Directory
このディレクトリはデフォルトでは存在していませんが、make:mail
Artisanコマンドを実行すると作成されます。Mail
ディレクトリには、アプリケーションから送信されるすべてのメールを表すクラスを設置します。メールオブジェクトを使用すると、メールを作成するすべてのロジックを、Mail::send
メソッドを使用して送信できる単一の単純なクラスにカプセル化できます。This directory does not exist by
default, but will be created for you if you execute
the make:mail
Artisan command. The
Mail
directory contains all of your
classes that represent
emails[/docs/{{version}}/mail] sent by your
application. Mail objects allow you to encapsulate
all of the logic of building an email in a single,
simple class that may be sent using the
Mail::send
method.
ModelsディレクトリThe Models Directory
Models
ディレクトリには、すべてのEloquentモデルクラスを設置します。Laravelが提供するEloquent
ORMは、データベースを操作するための美しくシンプルなActiveRecordの実装を提供しています。各データベーステーブルには、そのテーブル操作に使う対応する「モデル」があります。モデルを使用し、テーブル内のデータをクエリしたり、テーブルに新しいレコードを挿入したりできます。The Models
directory
contains all of your Eloquent model
classes[/docs/{{version}}/eloquent]. The
Eloquent ORM included with Laravel provides a
beautiful, simple ActiveRecord implementation for
working with your database. Each database table has
a corresponding "Model" which is used to
interact with that table. Models allow you to query
for data in your tables, as well as insert new
records into the table.
NotificationsディレクトリThe Notifications Directory
このディレクトリはデフォルトでは存在しませんが、make:notification
Artisanコマンドを実行すると作成されます。Notifications
ディレクトリには、アプリケーション内で発生するイベントに関する簡単な通知など、アプリケーションが送信するすべての「トランザクション」的な通知を設置します。Laravelの通知機能は、電子メール、Slack、SMSなどのさまざまなドライバを介して通知を送信したり、データベースに保存したりすることを抽象化しています。This directory does not exist by
default, but will be created for you if you execute
the make:notification
Artisan command.
The Notifications
directory contains
all of the "transactional"
notifications[/docs/{{version}}/notifications]
that are sent by your application, such as simple
notifications about events that happen within your
application. Laravel's notification feature
abstracts sending notifications over a variety of
drivers such as email, Slack, SMS, or stored in a
database.
PoliciesディレクトリThe Policies Directory
このディレクトリはデフォルトでは存在しませんが、make:policy
Artisanコマンドを実行すると作成されます。Policies
ディレクトリには、アプリケーションの認可ポリシークラスを設置します。ポリシーは、ユーザーがリソースに対して特定のアクションを実行できるかどうかを判断するために使用されます。This directory does not exist by
default, but will be created for you if you execute
the make:policy
Artisan command. The
Policies
directory contains the
authorization policy
classes[/docs/{{version}}/authorization] for
your application. Policies are used to determine if
a user can perform a given action against a
resource.
ProvidersディレクトリThe Providers Directory
Providers
ディレクトリは、アプリケーションの全サービスプロバイダにより構成します。サービスプロバイダは、サービスをコンテナと結合、イベントの登録、もしくはアプリケーションへやってくるリクエストを処理するために必要な用意をするタスクを実行するなど、アプリケーションの事前準備を行います。The Providers
directory contains all of the service
providers[/docs/{{version}}/providers] for
your application. Service providers bootstrap your
application by binding services in the service
container, registering events, or performing any
other tasks to prepare your application for incoming
requests.
インストール直後のアプリケーションでも、このディレクトリは多くのプロパイダーを含んでいます。必要に応じて、自分のプロバイダを自由に追加してください。In a fresh Laravel application, this directory will already contain several providers. You are free to add your own providers to this directory as needed.
RulesディレクトリThe Rules Directory
このディレクトリは、デフォルトでは存在していません。make:rule
Artisanコマンドを実行すると、作成されます。Rules
ディレクトリは、アプリケーションが使用するバリデーションルールオブジェクトで構成します。ルールは複雑なバリデーションロジックをシンプルなオブジェクトへカプセル化するために使用します。詳細は、バリデーションのドキュメントで確認してください。This directory does not exist by
default, but will be created for you if you execute
the make:rule
Artisan command. The
Rules
directory contains the custom
validation rule objects for your application. Rules
are used to encapsulate complicated validation logic
in a simple object. For more information, check out
the validation
documentation[/docs/{{version}}/validation].