Fork me on GitHub

Welcome

Disclaimer: Majority of the assets, design and format of this site is borrowed (forked might be the more better term) from the original PHP : The Right Way site. The original contributors is given all the credits due to them.

This site serves as a guide for those who is using Laravel Framework by Taylor Otwell and its contributors. Laravel : Best Practices aims to put together all the resources and best practices in using the Laravel Framework.

Laravel Framework is not strict on how and where you define your classes as long as it could be loaded by Composer. This is a double-sided blade – it’s beneficial for advanced developers, but coud be confusing for beginners and for those who is trying to reverse engineer an existing codebase. The ability to place anything, anywhere is good. But there are times some methods is way better than the others, and that is what we are after. This site tried to collect all the best practices, tools and methodologies, so that it could serve a guide for beginners and existing developers alike.

This is a living document and will continue to be updated with more helpful information and examples as they become available.

Translations

Laravel: The Best Practices is currently available in the following languages. Please contribute to the project so that it could support multiple languages.

How to Contribute

Help make this website the best resource for new PHP programmers! Contribute on GitHub

Spread the Word!

Laravel Best Practices has web banner images you can use on your website. Show your support, and let new PHP developers know where to find good information!

See Banner Images

Download the Android Application

Get it in Google Play

Back to Top

Getting Started

The Ecosystem

At the very minimum, you at least need to know how to use Git and Composer. Though not required, it is better that you have an account in GitHub.com as it is where all the codes and its dependencies is hosted.

For local development, you’ll need to have at least Vagrant and VirtualBox installed. This is used by Homestead (a special vagrant box made for running Laravel apps). Although you can use the traditional WAMP/MAMP/XAMPP stack, those are not officially supported, thus you might have hard time down the road.

Pure “Laravel-way” frontend development might be a daunting one as it got has a long chain of technologies. At the very top of the chain is Elixir, which is basically an API for GulpJS, which in turn requires npm which is basically a package manager for NodeJS.

CSS is managed either through Sass or LESS, while JavaScript is through CoffeeScript. You can write ES6/ES7 codes since Elixir also supports BabelJS. While NodeJS dependencies is then made available to browser through Browserify.

For backend stack, at the very least, you have Nginx, PHP-FPM and MySQL. Other optional stack component are Memcached, Redis and Beanstalkd.

While you are not required to understand them all at once. It is advantagous that you are at least familiar with these and do some reading what are those and where it is being used. This will save you tons of confusion when reading the documentation and references in the future.

Use the Current Stable Version (5.3)

If you are getting started with Laravel, start with the current stable release of Laravel 5.2. Laravel has added powerful new features over the last few years. If you are looking for a function or its usage, the documentation on the laravel.com/docs website will have the answer.

Homestead

Laravel is not only using Vagrant as the official development environment but also has its own base box called “Homestead”. You can use Homestead to start developing with Laravel without the hassle of setting up your local environment for Laravel.

The instructions and usage of homestead can be found in the Official Documentation.

Build your own Homestead

When you use Homestead, there are pre-defined softwares already installed. If you want to customize the installed softwares, you will need to have an access to the scripts that is used to build Homestead itself. This project is called “Settler”. You can check the source codes of that project to have a better understanding on how the Homestead was built, and probably add your own scripts as well.

Back to Top

Code Style Guide

The Laravel framework itself doesn’t impose standards on how you should name your variables or where to put files. As long as it can be loaded by Composer, it will technically run inside the application. However, developers are encouraged to study and follow the following PSR standards:

Back to Top

Framework Highlights

Programming Paradigms

Design Patterns

There are several design patterns that powers Laravel. Phill Sparks has wonderful presentation about this.

How Laravel Works?

Christopher Pitt has impressive explanation of the inner workings of Laravel, you can check it out in http://rebuildinglaravel.com/

Eloquent ORM

The most popular feature of Laravel is it’s rockstar ORM called Eloquent. It’s the easiest way to use an ORM in your application. You can read more about Eloquent in the official documentation.

Command Line Interface

Laravel comes with its own command line interface called Artisan. It is basically using the Symfony Console component as its foundation. Laracasts has good tutorials on understanding how commands works and how to make your own.

Debugging

Laravel’s cool error page is something that it is known for. Thanks for the Whoops library which powers the error page you are seeing whenever there is an error.

Note that the Whoops library has been removed from Laravel 5.

Back to Top

Dependency Management

Laravel is not built from scratch. It is built on top of several framework and libraries. You can see the listing of other libraries it is using here. It is also being powered by Illuminate components. All of these components are being tied into each other by making sure each of them can be treated and written as a composer Composer library. These libraries is hosted in public repositories.

Composer and Packagist

By default, it is using Packagist as repository and GitHub to download the files itself. But you can define your own repositories by setting up your own Satis mirror of packages. Although you can use any class or even plain php functions inside Laravel (as long as it can be autoloaded). This is not a recommended practice. Make your own Composer library for each application specific classes and include it as a dependency in the composer.json file.

Laravel Package Archivists

Following are the sites where you can find Laravel Packages:

Back to Top

Coding Practices

The Basics

Laravel has tons of features which might be overwhelming at first.

Here are some articles and resources that may be useful to get on the same page with Laravel.

Date and Time

Although PHP has a class named DateTime to help you when reading, writing, comparing or calculating with date and time. It is recommended that you use the Carbon library for dealing with dates.

Design Patterns

It is easy to get the your Laravel application to get messed up if you don’t have any sort of pattern to follow. The following design patterns are recommended for those who are building an application with Laravel or want to refactor their existing projects:

Working with UTF-8

Laravel uses the patchwork/utf8 for most of it’s UT8-related stuffs. It is required that all developers understands the following concepts:

Back to Top

Database Conventions

Eloquent can support multiple Database for a reason - Don’t limit yourself to MySQL.

Use MongoDB for records that has attributes that varies a lot. For example, in an inventory system, an office supplies product might have a different set of fields compared to vehicle and auto supplies.

Use ElasticSearch for high volume data searching and indexing.

Use Neo4J for applications that requires complex relationships between models. For example a multi-level networking application, social network site and alike.

Table and field naming

Table and field names MUST be lowercase and use Snake Case.

Table name should use the plural form of the actual real life object it is storing. Like for example, the table name for blog posts should be posts not post.

Primary keys should be named “id” in the table names. While the foreign key it represents in other table should be on singularform_id. (e.g. table: post, primary key: id, foreign key: post_id )

Changing tables, index or inserting sample data.

DONT create tables or index directly via PHPMyAdmin or console. Use database migration to create table, add/alter any fields, and commit those to Git repository.

DONT pass directly the database export (sql files ) to your colleagues in able to share your database changes, let them run the migration files you committed to the repository.

DONT insert fake values directly to the database for testing purposes. create Seeder files to populate your database.

Choose right database for the job at hand.

Eloquent can support multiple databases for a reason - Don’t limit yourself to MySQL.

Use MongoDB for records that has attributes that varies a lot. For example, in an inventory system, an office supplies product might have a different set of fields compared to vehicle and auto supplies.

Use ElasticSearch for high volume data searching and indexing. It is also useful on systems that requires sharding of data in multiple machines which is very huge to fit in a single replica.

Use Neo4J for applications that requires complex relationships between models. For example a multi-level networking application, social network site and alike. Although you can do the same in MySQL, the amount of joins when you go beyond 2-level parent-child relationships will be unbearable in any production environment.

Back to Top

Configuration

Always make sure your application key is set. This is APP_KEY variable in .env file. You can generate one via

php artisan key:generate

Always give your application a name. That is, instead of using the default App root namespace given by Laravel install, set it to what the application is all about. This can be set via

php artisan app:name YourAppName

This makes your controllers/models etc resolve into YourAppName\Controllers and YourAppName\Models.

Use .env files to store any secure information and retrieve it via getenv function. These should be no instance on which you will put it inside models/controllers and commit it to Git.

Back to Top

Resources

From the Source

People to Follow

Mentoring

PHP PaaS Providers

Components

Illuminate components which can be found in https://github.com/illuminate

are the smaller parts that makes up Laravel Framework. Several other open-source libraries can be also seen to be used by Laravel by mere looking at its composer.json file https://github.com/laravel/framework/blob/5.0/composer.json#L22-L43

Other Useful Resources

Laracasts

Youtube Channels

Books

Back to Top

Community

User Groups

Conferences

Back to Top