0

Comparison of Laravel and CakePHP [English]

Comparison of Laravel and CakePHP

Introduction

The majority of web developers use some type of frameworks to make easier developing their websites. PHP language has dozens or maybe even hundreds of them. A few years ago web developers preferred to use framework named CakePHP, but nowadays definitely most popular framework is Laravel.

These two frameworks have many similarities, however, they have also some differences.  What are their pros and cons? Which one is better to achieving the specific objectives and why? If nowadays Laravel is so popular it is still good decision to create projects in CakePHP?

History

In April 2005 polish programmer Michał Tatarynowicz created a base of a new framework. In the end of 2005 Cake Software Foundation has been founded and soon the first official version of CakePHP has been released. From the beginning the license of this framework was MIT.

CakePHP generally tries to preserve compatibility among minor versions of the same major version (1.x, 2.x, 3.x). Additionally, they provide comprehensive migration guide to each major version and also a tool that can make automatically the most part of the upgrade for us (however migration from 2.x to 3.x requires manual changing all methods and views that use database; it’s really horrible even with additional tools). Now stable major version of CakePHP is 3.3.

Laravel, however, was created in 2011 by Taylor Otwell. Certain components have been incorporated from Symfony framework. The first version had not contained controllers so in the few months has been created second the version that added handling real MVC and template engine. It also uses MIT license.

In this framework compatibility of each version is much worse when we compare it with CakePHP. Even smaller versions such as 5.1, 5.2 and 5.3 have some incompatibilities, and some functions are renamed or accept different parameters. The newest stable version of Laravel is 5.3 for now.

Basic concepts

Both frameworks use MVC (Model-View-Controller) architectural pattern, however, it is worthy to be mentioned that CakePHP has been using this since the first version (released in 2005), but Laravel since the second version (2011).

Every framework has some original ideas and tries to use at least one new concept in the core. Here is no difference: Laravel likes to use MVC, services, facades, middlewares and is obsessed with CSRF-protection. CakePHP, instead, much more prefers to use only MVC, helpers and additional plugins. Of course both of them use also other programming features, however, those are the most noticeable.

Laravel provides a complete authentication mechanism that is ready just after installation. It provides also substantially built-in tools to manage assets – Laravel Elixir. It helps with concatenation, compilation, and minification of assets. In CakePHP, for now, the best option to do it is using 3rd party plugins.

Database access & ORM

Both Laravel and CakePHP are using ORM (Object-Relational Mapping), however in my opinion in Laravel exists some inconsistency, because we have 2 options to manage database: Data Query and ORM. I think it should be unified. On the other hand, CakePHP introduced ORM very late, because in version 3.0 (2015), and probably it was one of the main reasons why it lost many followers. Also, ORM in CakePHP is slightly more complex.

Below is a simple example how to insert a new row to database using provided ORM. I assume that it has already existed model that contains required fields. I assign values to fields name and university on object $user and then I save it.

 

LaravelCakePHP
<?php
$user = new User; 
$user->name = "Józef Curyłło";
$user->university = "ULPGC"; 
$user->save();
?>

 

<?php
$usersTable = TableRegistry::get('Users');
$user = $usersTable->newEntity(); 
$user->name = 'Józef Curyłło';
$user->university = 'ULPGC'; 
$usersTable->save($user);
?>

 

Other basic operations such as reading from database, update records or remove them are also really easy and intuitive; however when we use many complex relationships, in my opinion, it can be a bit easier to do it in Laravel  (actually in Laravel we can create relationship method as we want. In CakePHP we need to use predefined ones). On the other hand, I found some problems with getting results from basic queries using Laravel, probably because of inconsistency between Eloquent ORM and Query Builder. I needed some time to gain experience and then it was much easier, however, I still had “inconsistency” in my mind.

Templates

Laravel uses Blade template engine. It’s really good idea which allows us to leave the structure of MVC intact. It is also very nice connected with controllers and I had no problem with using sub-templates or sharing variables between them. When we really need we can also use PHP in this files (variables are exactly the same).

CakePHP normally uses an own type of template engine that interprets files with extension .ctp. In reality, it is just a normal PHP file and we can use some predefined useful methods to maintain it as well as normal PHP functions so it can be also advantage and disadvantage because it can help us a lot and we can also spoil structure of MVC. When we try to make complex view our file may become very messy and difficult to read. We also are able to set different template engine, e.g. TWIG, however, it does not default behavior and may be slightly worse in performance and in working correctly.

Below I share an example of a simple foreach loop, which should iterate over collection $users and print sentence with an id of every element in this collection.

Laravel (Blade)CakePHP
@foreach ($users as $user) 
 <p>This is user {{ $user->id }}</p>
@endforeach


 

<?php foreach ($users as $user): ?>

  <p>This is user <?= $user->id ?></p>

<?php endforeach; ?>

 

Pros and cons of each framework

In my opinion, the main disadvantage of CakePHP was a lack of support for a tool named Composer even when CakePHP was in version 2. It also resulted in some problems connected with modularity and using 3rd party plugins. Also very important was a lack of ORM until version 3 and uncomfortable template engine. I think that authors need a new look to this framework.

In Laravel very irritating is endless changing names and parameters of methods even in minor versions of the framework. Some parts should be unified and more consequent. Error handling also isn’t perfect: we obtain a long stack trace, however often it isn’t very helpful. Sometimes we aren’t able to localize file with the error using only this stack trace(!). On the other hand, Laravel has very nice test module – you can use unit tests in an easy and friendly way.

Both of them have also many pros, e.g. MVC, ORM, or plugins. CakePHP, in my opinion, can be a bit easier to learn, because it doesn’t use so many features as Laravel, but on the other hand CakePHP can be less efficient, especially in big projects and more difficult to maintain.

Despite some issues and cons, I think that Laravel is the better option when we are looking for a new framework to our project. The community of this framework is also very important and obviously, it is the biggest one among all frameworks. Already written module of authentication is also a nice and important advantage of Laravel. When we need to maintain project already written in some version of CakePHP it will be much better and easier to still write it in CakePHP. It is still good framework and can be used especially when we prefer to have a more consistent platform.

Bibliography

  1. https://laravel.com/docs/5.3/
  2. https://book.cakephp.org/3.0/en/index.html
  3. https://book.cakephp.org/1.1/en/introduction-to-cakephp.html
  4. https://en.wikipedia.org/wiki/Laravel
  5. https://www.sitepoint.com/best-php-framework-2015-sitepoint-survey-results/

P.S. This article has been made as short tasks at the Universidad de Las Palmas de Gran Canaria when I was studying there.

Dodaj komentarz

Twój adres email nie zostanie opublikowany. Pola, których wypełnienie jest wymagane, są oznaczone symbolem *