イントロダクション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フォルダーは、フレームワークの初期起動やオートローディングの設定を行うファイルを含んでいます。The bootstrap folder contains a few files that bootstrap the framework and configure autoloading.
configディレクトリーには、名前が示す通り、アプリケーションの全設定ファイルが設置されています。The config directory, as the name implies, contains all of your application's configuration files.
databaseフォルダーは、データベースのマイグレーションと初期値設定(シーディング)を配置します。The database folder contains your database migration and seeds.
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 "language" files.
storageディレクトリーへは、コンパイルされたBladeテンプレート、ファイルベースのセッション、ファイルキャッシュなど、フレームワークにより生成されるファイルが保存されます。The storage directory contains compiled Blade templates, file based sessions, file caches, and other files generated by the framework.
testsディレクトリーには、皆さんの自動テストを配置します。The tests directory contains your automated tests.
vendorディレクトリーへは、Composer依存パッケージが配置されます。The vendor directory contains your Composer 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, filters, and requests.
Commandsディレクトリーはもちろん、アプリケーションのコマンドを置いておく場所です。Commandsはアプリケーションにより、キューに投入されるジョブを表すと同時に、現在のリクエストのライフサイクル中に、同期的に実行できるタスクを表します。The Commands directory, of course, houses the commands for your application. Commands represent jobs that can be queued by your application, as well as tasks that you can run synchronously within the current request lifecycle.
Eventsディレクトリーは、見ての通りイベントクラスを設置する場所です。もちろん、イベントとして扱うクラスを使用することは必須ではありません。しかし、使用することを決めた場合、Artisanコマンドラインにより生成されるクラスは、デフォルトでこのディレクトリーへ設置されます。The Events directory, as you might expect, houses event classes. Of course, using classes to represent events is not required; however, if you choose to use them, this directory is the default location they will be created by the Artisan command line.
Handlersディレクトリーは、コマンドとイベント両方のハンドラークラスで構成されています。ハンドラーは、コマンドかイベントを受け取り、コマンドか発行されたイベントに応じたロジックを実行します。The Handlers directory contains the handler classes for both commands and events. Handlers receive a command or event and perform logic in response to that command or event being fired.
Servicesディレクトリーは、アプリケーションを機能させるのに必要な、数々の「ヘルパ」サービスで構成します。例えば、Laravelに含まれるRegistrarサービスは、アプリケーションの新しいユーザーのバリデーションと生成の役割を担っています。サービスの他の例には、外部APIやメトリックシステムと連携したりするものや、自分自身のアプリケーションから情報を収集するものまで上げられます。The Services directory contains various "helper" services your application needs to function. For example, the Registrar service included with Laravel is responsible for validating and creating new users of your application. Other examples might be services to interact with external APIs, metrics systems, or even services that aggregate data from your own application.
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 theappdirectory can be generated by Artisan via commands. To review the available commands, run thephp artisan list makecommand 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