29.07.2016 Views

laravel-5

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Application Structure 116<br />

The tests directory contains your automated tests. An example PHPUnit⁵¹ is provided out of the<br />

box.<br />

The vendor directory contains your Composer⁵² dependencies.<br />

The App Directory<br />

The “meat” of your application lives in the app directory. By default, this directory is namespaced<br />

under App and is autoloaded by Composer using the PSR-4 autoloading standard⁵³. You may change<br />

this namespace using the app:name Artisan command.<br />

The app directory ships with a variety of additional directories such as Console, Http, and Providers.<br />

Think of the Console and Http directories as providing an API into the “core” of your application.<br />

The HTTP protocol and CLI are both mechanisms to interact with your application, but do not<br />

actually contain application logic. In other words, they are simply two ways of issuing commands<br />

to your application. The Console directory contains all of your Artisan commands, while the Http<br />

directory contains your controllers, filters, and requests.<br />

The Jobs directory, of course, houses the queueable jobs for your application. Jobs may be queued<br />

by your application, as well as be run synchronously within the current request lifecycle.<br />

The Events directory, as you might expect, houses event classes. Events may be used to alert other<br />

parts of your application that a given action has occurred, providing a great deal of flexibility and<br />

decoupling.<br />

The Listeners directory contains the handler classes for your events. Handlers receive an event and<br />

perform logic in response to the event being fired. For example, a UserRegistered event might be<br />

handled by a SendWelcomeEmail listener.<br />

The Exceptions directory contains your application’s exception handler and is also a good place to<br />

stick any exceptions thrown by your application.<br />

Note: Many of the classes in the app directory can be generated by Artisan via commands.<br />

To review the available commands, run the php artisan list make command in your<br />

terminal.<br />

Namespacing Your Application<br />

As discussed above, the default application namespace is App; however, you may change this<br />

namespace to match the name of your application, which is easily done via the app:name Artisan<br />

command. For example, if your application is named “SocialNet”, you would run the following<br />

command:<br />

⁵¹https://phpunit.de/<br />

⁵²https://getcomposer.org<br />

⁵³http://www.php-fig.org/psr/psr-4/

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!