PHP Frameworks Comparison: Yii vs Laravel
Many years ago our company was working on a big project - a Real Estate Portal. It was going to be a high-load web app, and we needed to find the best high-performance framework for it. At the time I didn’t have much prior experience, I had mostly worked with CMS before. But a CMS was a bad fit for this particular project, as we were planning to use many non-standard components. Moreover, I never liked working with CMS, because it’s often hard to manipulate their source code. I decided to conduct an Internet research and read many comparative posts and reviews about the cutting edge frameworks of that time. As a result, I chose Yii. The decisive factors were perfect documentation, a load of dedicated forum discussions, popularity and, therefore - low entry barrier.
I remember, as if it was yesterday, that I didn’t have many difficulties and I could “google” a solution for almost any problem. Of course, my code wasn’t perfect and I implemented many things roughly and improperly, but it stemmed from the fact that I lacked experience with frameworks in general. I simply had no knowledge of more elegant and proper implementation.
Now, many years later, I decided to try out a framework that is popular today - Laravel. I can tell you right away: it was uncomfortable to work with, but when it comes to dealing with something new - it’s a common issue. Note, that the documentation is laconic, but I really felt like it's missing detailed descriptions of little things. Because of that, I had to go through the source code pretty often. Moreover, Laravel, unlike Yii, is rich in abstracts, so executed methods weren't easy to find. All in all, it seems like Laravel is similar to Yii in terms of usage, but implementation methods differ and it takes time to get comfortable. Something is more convenient, something - on the contrary, provokes wrath and fury. Now, let's get to our comparison.
Description
The first impression is strongly influenced by the description on the official website. Let’s take a look at what these frameworks have to offer (at the moment):
Yii 2Yii is a high-performance PHP framework that is perfect for swift development of modern web applications. Its possibilities allow implementing large scale projects like forums, portals, CMS, RESTful web services, etc.
As most frameworks, Yii is based on MVC (Model–view–controller) pattern.
The core of Yii philosophy is simple and elegant code.
Yii is a full-stack framework that provides multiple turnkey solutions: a query builder, ActiveRecord for relative and NoSQL databases, RESTful API, multi-level caching support and many other things.
Yii is very scalable. You can modify or redefine any piece of source code. You can also create your own extensions.
The main purpose of Yii is to provide high-performance.
Laravel is a framework designed for building web applications with exquisite and sophisticated syntax. It simplifies dealing with sore spots, such as authentication, routing, sessions and caching. Laravel was conceived to embody the best of all PHP and other frameworks (like Ruby on Rails, ASP.NET MVC and Sinatra). Laravel is accessible, yet powerful. It comes with many excellent tools for building large and reliable apps:
Superb IoC (Inversion of control).
Convenient migration system.
Integrated system of module testing.
Sounds great, right? But if you draw a side-by-side comarison, you’ll see that each of them has certain strengths and weaknesses. To be honest, comparing them is like comparing NetBeans IDE and PhpStorm IDE: both have broad possibilities implemented in a different way. Until you try - you can’t really understand which one works for you the best.
Yii vs Laravel
As known, in order to compare two tools you need to compare the way most common development capabilities are implemented in each of them. I really tried to take all crucial aspects into consideration:
Parameter | Yii | Laravel |
Requirements | PHP 5.4 or higher |
|
MVC (Model–view–controller) |
Yes | Yes |
Extensions | Yes | Yes |
ORM (Object-relational mapping) |
|
|
Testing | Out of the box:
|
|
Debugging |
|
Imho, pretty limited, but the problem can be solved with the laravel-debugbar extension. |
Migration | Migration tool (Migration class) |
|
Security |
Everything off-the-shelf + plugins. Component access control is closely integrated with RBAC. |
|
Pattern generators, working with patterns |
|
|
Caching |
|
|
REST API |
|
|
App localization | Yes | Yes |
Form validation | Yes | Yes |
Scaffolding |
|
|
Some parts I’d like to talk more about:
ExtensionsWell, both frameworks are well equipped with plenty of extensions for almost anything. It’s important, because all disadvantages can be mediated with the help of one or two extensions. And they fit like a glove.
MigrationsBoth frameworks offer convenient migration tools, but Laravel also enables you to add seeders for simple and smooth initial data transfer. It comes in handy for both testing (test data) and filling static directories. With Yii you only have Migrations to do that.
SecurityI was, actually, surprised that Laravel doesn’t have enough off-the-shelf access control tools. Nevertheless, there are many extension packages for that. I have to acknowledge that both frameworks offer instuments for working with passwords, authentication, protection from SQL injections, Cross Site Scripting (XSS), Cross Site Request Forgery (CSRF), etc.
Form validationBoth frameworks, obviously, include validation, but it’s implemented differently. In Yii it’s linked to the class of a form or a model. It allows you to set up rules directly in it and check it according to those rules after receiving data and filling in ActiveRecord. Let’s say, there’s another form that changes a portion of model data. In this case, you’ll have to either create a specific class for it, or use validation scripts. It’s not a problem, unless you need to validate some values directly within the controller and create your custom verification method, and not to use a ready-made solution. That’s something Laravel can really be proud of. Its validator exists as a separate assisting class. You can validate data any place, any time. Plus, for convenience and unification purposes you can extend Eloquent class with your own methods.
Results
From what I’ve seen on multiple forums, people, who started working with Laravel and tried to master Yii later claimed that it was uncomfortable and odd to deal with. And vice versa. At the end of the day, there’s no clear winner here. It’s up to you to decide which one suits you best.