Please note that this chapter was written for VERSION 3 of the Laravel PHP Framework.
Laravel's source package contains a number of different directories. Let's take a look at the project structure to gain a greater understanding of how things work. I may use some terms to describe various features of Laravel that could be confusing if you are just starting out, if so, bear with me as we will cover each feature in more detail in a later chapter.
Root Directory Structure
Lets take a quick look at the top-level file and directory structure :
/application /bundles /laravel /public /storage /vendor /artisan [file] /paths.php [file]
Now lets take a closer look at each item :
This is where the majority of the code for your application will live. It contains your routing, data models and views. You will be spending most of your time here!
Bundles are Laravel applications. They can be used to separate aspects of your application, or can be released / downloaded to share common code. By installing new bundles with artisan, you can extend the functionality of Laravel to suit your needs.
/application directory is also a bundle known as the
DEFAULT_BUNDLE, this means that anything you use in
/application you can also use in your bundles!
This is where the framework's core files live. These are the files it needs to execute a request. You will rarely have to interact with this directory, but it can sometimes be useful to browse the source to see how a Class or Method works. Alternatively you could check the Laravel API.
This is the directory that you must point your web server to. It contains the bootstrap file
laravel subdirectory also contains the files needed to render the off-line documentation correctly.
The storage directory is used as file store for services that use the file system as a driver, for example Sessions, or the Cache class. This directory must be writeable by the web server. You will not need to interact with this directory to build a Laravel application.
The vendor directory contains code used by Laravel, but wasn't written by the framework's author or contributors. The directory contains open source software, or parts of software that contribute to Laravel's features.
Artisan is Laravel's Command Line Interface. It allows you to perform numerous tasks on the command line. You can even create your own tasks! To run Artisan simply type:
This file is used by the framework to determine paths to the important directories mentioned above, and to provide a short-cut for retrieving them ( using
path()). You should not need to edit this file.
Application Directory Structure
As mentioned above,
/application is where all the fun happens, so let's have a look at the structure of the
/config /controllers /language /libraries /migrations /models /tasks /tests /views /bundles.php [file] /routes.php [file] /start.php [file]
And now a closer look at each one.
The config directory contains a number of configuration files for changing various aspects of the framework. No configuration needs to be set at install for the framework to work 'out of the box'. Most of the configuration files return key-value PHP arrays of options, sometimes key-closure pairs that allow a great deal of freedom to modify the inner working of some of Laravel's core classes.
Laravel provides two methods for routing, using
controllers and using
routes to closures. This directory contains the Controller classes that are used to provide basic logic, interact with data models, and load view files for your application. Controllers were added to the framework at a later date to provide familiar ground for users migrating from other frameworks. Although they were added as an afterthought, due to Laravel's powerful routing system, they allow you to perform any action which can be performed using
routes to closures.
In this directory, PHP files containing arrays of strings exist to enable easy localization of applications built using Laravel. By default the directory contains string files for pagination and form validation in the English language.
The libraries directory can be used to 'drop in' single class PHP Libraries to provide extra functionality for your application. For larger Libraries it is recommended that you create a Bundle instead. The libraries directory is added to the Autoloader at startup from the start.php file.
The migrations directory contains PHP classes which allow Laravel to update the Schema of your current database, or populate it with values while keeping all versions of the application in sync. Migration files must not be created manually, as their file name contains a timestamp. Instead use the Artisan CLI interface command
php artisan migrate:make <migration_name> to create a new Migration.
Models are classes that represent your project's data. Normally this would mean integrating with a form of database, or other data source. Laravel provides three methods for interacting with common database platforms, including a query builder named 'Fluent', which allows you to create SQL queries by chaining PHP methods. You could also use the Eloquent ORM to represent your tables as PHP Objects, or use the plain old raw SQL queries that you're used to. Fluent and Eloquent both use a similar syntax, making their adoption a smooth transition.
Files in the models directory are auto-loaded automatically from
By creating classes in the tasks directory, you are able to add your own custom tasks to the Laravel Artisan command line interface. Tasks are represented by classes and methods.
The tests directory provides a location for you to keep your application unit tests. If you are using PHPUnit, you can execute all tests at once using the Laravel Artisan PHP command line interface.
The views directory contains your HTML template files to be used by controllers or routes, although please use a .php extension for files in this directory. You can alternatively use a .blade.php extension to enable parsing with the Blade templating library which will be explained in a later chapter.
To enable a bundle, simply add it to the array in bundles.php. You can also use a key-value, name-array pair to define extra options for the bundle.
The routes file contains the methods which enable routes to be mapped to their appropriate outcome with Laravel. This topic will be explained more thoroughly in upcoming chapters. This file also contains declarations for several events including error pages, and can be used to define view composers or route filters.
The start.php contains start-up routines for the
/application bundle, such as auto-loading directories, loading configuration files, and other wonderful useful stuff! Feel free to append to this file.
In the next chapter we will be cover routing using controllers.
My books are available online for free to encourage learning. However, if you'd like for me to keep writing, then please consider buying a digital copy over at Leanpub.com.
It's available in PDF, ePub, and Kindle format, and contains a bunch of extras that you won't find on the site. I have a full-time job, and I write my books in my spare time. Please consider buying a copy so that I can continue to write new books from the comfort of my sofa!