homepage >> Book review : Laravel design patterns    
 

Yossef Benharosh is an apt web developer and the author of the eBook The essentials of object oriented PHP.

Yossef Benharosh web developer profile linkedin twitter github

Book review : Laravel design patterns and best practices by Kılıçdağı, A & Yilmaz, HI 2014

I have chosen to read the book Laravel design patterns and best practices (a little more than 100 pp) due to the combination of areas important to the modern PHP developer: Laravel, on the one hand and design patterns, on the other hand. Laravel, for those of you who still do not know, is a modern PHP framework, which is similar to older frameworks like, Codeigniter, Zend and cake php. A developer may encounter design patterns when he begins to ask questions such as how to improve the code to make it more readable and easier to maintain and upgrade? Or is there an established and well-tested way to write a certain code?

 

The combination of Laravel and design patterns is essential for the understanding of the framework, because Laravel is applying design patterns at its’ core.

עטיפת הספר Laravel design patterns and best practices

First and foremost, Laravel implements the MVC design pattern, which is the most important pattern for PHP developers, due to the fact that it forces developers to resist the temptation of mixing logic in the view files. Full implementation of the MVC pattern in Laravel, separates the application into three layers: the model layer, which is responsible for interacting with the database, the view layer which displays the information to the users, and the dominant controller layer that connects the two other layers.

Another prominent example for using design patterns in Laravel, is the use of the Façade design pattern all over the place in the framework. The Façade, hides a lot of code behind the methods that appear simple, and do a lot of work without bothering the programmer. For example, if we want to pull the contents of the session, we only need to write the following simple looking code, that only looks like a static method:

    
    $foo = Session::get('foo');

In fact, Laravel does all the work for us behind the scenes, including: creating an instance of the class, injecting dependencies through the IoC container, and calling other methods, to mention just a few of the important things that Laravel does seamlessly for us.

The book contains six chapters, that explain many aspects of design patterns in Laravel:

The first chapter explains what are design patterns, and groups the design patterns into three main groups : creational patterns, structural patterns and behavioral patterns.

The next three chapters are devoted to the implementation of the MVC pattern in Laravel, a chapter for each of the three layers. The model layer, that handles the database, explains how ordinary queries can be written using the query builder, or even better with the Eloquent ORM that Larvel offers. The Eloquent ORM handles the database through models - a model for each table, it also handles the relationships between the various tables and models, and offers version control. The chapter on the view layer explains the structure of the template files and template engine, which is used to output HTML. Finally, the chapter on the controller layer, explains how the controller is connecting the two other layers, and how, among other things, it is responsible for receiving requests from the URL, and having them use methods that handle them.

The chapter before the last explains and demonstrates the use that Taylor Otwell, the creator of the framework, made in design patterns in developing the framework. And only the last chapter examplifies the writing of an original design patterns based code in a Larvel application.

As mentioned above, I have chosen to read this book due to the importance of design patterns in the architecture of Laravel. I did find in this book enough references to the subject, to allow me to see it as a good starter for every Laravel enthusiast who is looking to improve his understanding of the framework while improving his coding skills.

Kılıçdağı, A & Yilmaz, HI 2014, Laravel design patterns and best practices, Packt, Birmingham, Mumbai.

To the book: https://www.packtpub.com