イントロダクションIntroduction
Laravelのデフォルトアプリケーション構造はアプリケーションの大小にかかわらず、素晴らしいスタートを切ってもらえることを意図しています。もちろんアプリケーションは皆さんのお好みに応じ、自由に体系立ててください。クラスがComposerによりオートローディングできるならば、Laravelはクラスをどこに配置するか強制することはまずありません。The default Laravel application structure is intended to provide a great starting point for both large and small applications. Of course, 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.
ルートディレクトリThe Root Directory
新しくインストールしたLaravelのルートディレクトリーには、様々なフォルダーが用意されています。The root directory of a fresh Laravel installation contains a variety of folders:
app
ディレクトリーは皆さんが想像している通り、皆さんのアプリケーションのコアコードを配置します。このフォルダーの詳細は、このあと説明します。The app
directory,
as you might expect, contains the core code of your
application. We'll explore this folder in more
detail soon.
bootstrap
フォルダーはフレームワークの初期起動やオートローディングの設定を行うファイルを含んでいます。その中のcache
フォルダーは初期処理のパフォーマンスを最適化するため、フレームワークが生成するいくつかのファイルが保存されるフォルダーです。The bootstrap
folder
contains a few files that bootstrap the framework
and configure autoloading, as well as a
cache
folder that contains a few
framework generated files for bootstrap performance
optimization.
config
ディレクトリーには名前が示す通り、アプリケーションの全設定ファイルが設置されています。The config
directory, as the name implies, contains all of your
application's configuration files.
database
フォルダーはデータベースのマイグレーションと初期値設定(シーディング)を配置します。ご希望であれば、このファイルをSQLiteデータベースの設置場所としても利用できます。The database
folder
contains your database migration and seeds. If you
wish, you may also use this folder to hold an SQLite
database.
public
ディレクトリーはフロントコントローラーとアセット(画像、JavaScript、CSSなど)を配置します。The public
directory
contains the front controller and your assets
(images, JavaScript, CSS, etc.).
resources
ディレクトリーはビューやアセットの元ファイル(LESS、SASS、CoffeeScript)、「言語」ファイルを配置します。The resources
directory contains your views, raw assets (LESS,
SASS, CoffeeScript), and localization
files.
storage
ディレクトリーにはコンパイルされたBladeテンプレート、ファイルベースのセッション、ファイルキャッシュなど、フレームワークにより生成されるファイルが保存されます。このフォルダーはapp
、framework
、logs
ディレクトリーに分かれています。app
ディレクトリーはアプリケーションにより使用されるファイルを保存するために利用します。framework
ディレクトリーはフレームワークが生成するファイルやキャッシュに利用されます。最後のlogs
ディレクトリーはアプリケーションのログファイルが保存されます。The storage
directory contains compiled Blade templates, file
based sessions, file caches, and other files
generated by the framework. This folder is
segregated into app
,
framework
, and logs
directories. The app
directory may be
used to store any files utilized 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.
tests
ディレクトリーには皆さんの自動テストを配置します。サンプルのPHPUnitテストが最初に含まれています。The tests
directory
contains your automated tests. An example
PHPUnit[https://phpunit.de/] is provided out
of the box.
vendor
ディレクトリーには、Composerによる依存パッケージが配置されます。The vendor
directory
contains your
Composer[https://getcomposer.org]
dependencies.
AppディレクトリーThe App Directory
皆さんのアプリケーションの「肉」の部分は、app
ディレクトリー内に配置します。このディレクトリーはデフォルトで、App
名前空間のもとに置かれており、PSR-4オートローディング規約を使い、Composerがオートロードしています。app:name
Artisanコマンドを使用し、この名前空間を変更できます。The "meat" of your
application lives in the app
directory.
By default, this directory is namespaced under
App
and is autoloaded by Composer using
the PSR-4 autoloading
standard[http://www.php-fig.org/psr/psr-4/].
You may change this namespace using the
app:name
Artisan
command.
app
ディレクトリーは多様なサブディレクトリーを持っています。Console
、Http
、Providers
などです。Console
とHttp
ディレクトリーは、アプリケーションの「コア」へAPIを提供していると考えてください。HTTPプロトコルとCLIは両方共にアプリケーションと相互に関係するメカニズムですが、実際のアプリケーションロジックではありません。言い換えれば、これらはアプリケーションに指示を出す、2つの方法に過ぎません。Console
ディレクトリーは全Artisanコマンドで構成され、一方のHttp
ディレクトリーはコントローラーやフィルター、リクエストにより構成されています。The app
directory
ships with 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 simply
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.
Jobs
ディレクトリーはアプリケーションのキュー投入可能なジョブを置いておく場所です。Jobsはアプリケーションによりキューに投入されるジョブを表すと同時に、現在のリクエストのライフサイクル中で定期的に実行するタスクを表します。The Jobs
directory,
of course, houses the queueable
jobs[/docs/{{version}}/queues] for your
application. Jobs may be queued by your application,
as well as be run synchronously within the current
request lifecycle.
Events
ディレクトリーは名前の通り、イベントクラスを設置する場所です。イベントは特定のアクションが起きたことをアプリケーションの別の部分に知らせるために使われ、柔軟性と分離性を提供しています。The Events
directory, as you might expect, 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.
Listeners
ディレクトリーはイベントのハンドラークラスを設置します。ハンドラーはイベントを受け取り、発行されたイベントに対応するロジックを実行します。たとえばUserRegistered
イベントはSendWelcomeEmail
リスナーにより処理されるでしょう。The Listeners
directory contains the handler classes for your
events. Handlers receive an event and perform logic
in response to the event being fired. For example, a
UserRegistered
event might be handled
by a SendWelcomeEmail
listener.
Exceptions
ディレクトリーはアプリケーションの例外ハンドラーと、さらに自分のアプリケーションから投げる例外を置いておくには最適でしょう。The Exceptions
directory contains your application's exception
handler and is also a good place to stick any
exceptions thrown by your application.
注目:
app
ディレクトリー中の多くのクラスが、Aritsan コマンドにより生成できます。使用できるコマンドを確認するには、端末で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.
アプリケーションの名前空間付けNamespacing Your Application
前述の通り、デフォルトのアプリケーション名前空間はApp
です。しかし、app:name
Artisanコマンドにより簡単に変更することもできます。例えば、アプリケーションの名前が"SocialNet"なら、以下のようにコマンドを実行します。As discussed above, the default
application namespace is App
; however,
you may change this namespace to match the name of
your application, which is easily done via the
app:name
Artisan command. For example,
if your application is named "SocialNet",
you would run the following command:
php artisan app:name SocialNet
もちろん、シンプルなApp
名前空間のままにしておくのも自由です。Of course, you are free to simply
use the App
namespace.