Book Image

Laravel Design Patterns and Best Practices

By : Arda Kılıçdağı, H. İbrahim YILMAZ
Book Image

Laravel Design Patterns and Best Practices

By: Arda Kılıçdağı, H. İbrahim YILMAZ

Overview of this book

<p>This book covers how to develop different applications and solve recurring problems using Laravel 4 design patterns. It will walk you through the widely used design patterns—the Builder (Manager) pattern, Factory pattern, Repository pattern, and Strategy pattern—and will empower you to use these patterns while developing various applications with Laravel. This book will help you find stable and acceptable solutions, thereby improving the quality of your applications. Throughout the course of the book, you will be introduced to a number of clear, practical examples about PHP design patterns and their usage in various projects. You will also get acquainted with the best practices for Laravel that will greatly reduce the probability of introducing errors into your web applications.</p> <p>By the end of the book, you will be accustomed to the best practices and the important design patterns used in Laravel to make a great website.</p>
Table of Contents (13 chapters)

The Builder (Manager) pattern


This design pattern aims to gain simpler, reusable objects. Its goal is to separate bigger and more convoluted object construction layers from the rest so that the separated layers can be used in different layers of the application.

The need for the Builder (Manager) pattern

In Laravel, the AuthManager class needs to create some secure elements to reuse with selected auth storage drivers such as cookie, session, or custom elements. To achieve this, the AuthManager class needs to use storage functions such as callCustomCreator() and getDrivers() from the Manager class.

Let's see how the Builder (Manager) pattern is used in Laravel. To see what happens in this pattern, navigate to the vendor/Illuminate/Support/Manager.php and vendor/Illuminate/Auth/AuthManager.php files, as shown in the following code:

   public function driver($driver = null)
   {
      ...

   }



   protected function createDriver($driver)
   {
      $method = 'create'.ucfirst($driver).'Driver...