Yii 2.0 vs. Laravel

****Update February 8, 2017****

In 2015, with the release of Laravel 5.1, I switched all of my php development onto Laravel. Although I started with Yii2, and wrote the Yii 2 for Beginners book, I have since focused solely on Laravel, and written 3 books on Laravel, the latest being Laravel 5.4 For Beginners.

I outlined the reasons for my switch in the following post.  I had a great learning experience with Yii 2, for which I’m grateful, but I thought it was important  to put this post in context.

I wrote this when I was a complete beginner as a programmer, in 2014.  I’m leaving it up because it is part of my evolution as a writer and programmer.

The original post from 2014:

It was my job a couple of years ago to be a part of the team that decided what php framework my company was going to use.  We did as much research as we could and boiled it down to a choice between Symfony 2 and Yii 1.1.14.  We ultimately decided to go with Yii.  It seemed more agile for our project and we felt the learning curve would be more acceptable.  We did not regret our decision and we have been happy with the results.

Since we made that choice, I have followed the php frameworks to keep up on them.  At the time we made our decision, Laravel was so new, we couldn’t seriously consider it, but it has grown quite a bit since it’s 4.1 release.

In fact it’s grown so much that Laravel has become the most popular php framework.  There are some good reasons for this, including the fact that Taylor Otwell, the author of the framework, developed a very intuitive syntax that is very easy to understand, especially for beginners.

The Laravel framework is built on top of Symfony 2, a popular enterprise framework that is used for developing enterprise-level applications by many developement teams.  Symfony 2 is a robust framework, but I found it to be overly complicated and bloated on resources.  This makes Laravel even more bloated, since it’s layered on top of Symfony.

Although Symfony 2 has beautifully written documentation and an arhcitecture that aspires to be symphonic (pardon the pun), it comes with a steep learning curve, and I didn’t find reliable online tutorials/examples that would show me, for example, how to build a functioning user model.  It was my frustration in trying to learn Symfony that led me to Laravel.  Laravel seems to have more support, more books and a site called Laracasts.com that can get you up and running quickly.

Laravel is written very intuitively, using facades, which are a type of static method, making the code easy to follow.  If I could get inside Taylor Otwell’s head, I could imagine that his framework started as a series of shortcuts for Symfony that made it easier to work with.

Sometimes working with a particular framework is simply a preference for style, since they are all powerful tools in the hands of masterful coders.  I don’t wish to paint any of these frameworks in a negative light.  Programmers, however, constantly have to make decisions and deciding what framework to use is one of the most important decisions to be made.

So now we’re ready to discuss Yii.  I have to say that in comparison to Symfony 2 and Laravel 4.1, Yii 1.1.14 is just plain ugly.  Very powerful and in some ways much easier to use, but not necessarily intuitive or easy to learn.  They do have a very active and helpful community, but their documentation is typically lacking in sample code, a complaint I could make about all frameworks.  Laravel seems to do better on that front, although they are far from complete in that sense.

But Yii is different in more meaningful ways.  Both Symfony 2 and Laravel 4.1 embrace migrations to create database tables, which leads to a piecemeal approach to the data model.  Imagine six different programmers are deciding what tables to create and what attributes (columns) should be in those tables.  Though it’s a popular style of programming these days, it’s not in my opinion a best practice.

Database modeling should be a centralized and well-thought out task.  It should be built before any programming is done, perhaps not entirely, but to the point where the project is well-formed before a single line of code is written.  Relationships, foreign keys, indexes, etc. need to be part of a disciplined architecture, not subject to the whims of individual programmers.

Even in the case where a single programmer is  building the project, they are better off doing the database work with MySql Workbench (assuming MySql is the db) than with migrations.  For one thing, migrations might not enforce constraints on foreign keys correctly and you would not even be aware of the problem.

Ok, I could go on and on about DB modeling, but let’s get back to Yii.  Yii has a browser-based code-generation tool, named Gii, that lets you build your models from the existing database tables.  This allows you to develop the database inside of workbench first, then press a button later to generate the models based on your well-thought out DB design.  Yii also supports migrations if you insist on creating your tables that way.

The code generation doesn’t stop with models, it also allows you to build an entire crud scaffold, with ready-made controllers and views.  This means you can develop a basic application quickly, then focus on the things that make your application unique, the complicated stuff.

While Yii 1.1.14 was powerful, it wasn’t necessarily beautiful.  Now some programmers will simply dismiss this as a cosmetic notion because after all, what does it matter?  Well, the more cryptic the architecture, the slower people learn and the slower projects move, which is never a good thing.  Also, there were some other problems, the most notable of which were the fact that they didn’t use composer for dependency management and didn’t use namespaces.

To their credit, the Yii team, led by founder Qiang Xue, listened to their community and with Yii 2.0 patiently built the most intuitive and full featured php framework I have ever seen.  Imagine out of the box that you have a frontend, backend, a functioning user model with signup and forgot password functionality fully working.  Imagine the framework uses Bootstrap 3 as its default css framwork.  Can you believe you get mobile first design right out of the box?  Well, no need to wonder, just download the advanced template and try it for yourself, you will be amazed at all the features and how fast you will move through it.  Or I should say, just use composer to install it because it now uses composer to manage its dependencies and composer is really easy to use.

It’s hard to describe the beauty of the architecture, you just have to see it for yourself, but it is much more concise and intuitive than it was before.  As of this post, Yii 2.0 is still in alpha, but the beta is coming quick.  Not a lot of docs yet, but you can find what they have here:  http://stuff.cebe.cc/yii2docs/guide-index.html.  My bet is that you will be hearing a lot about Yii in the next year or so as it releases into production.

If you have a preference for one of the three frameworks in this post, please take our poll and feel free to sound off in the comments.

Advertisements

6 thoughts on “Yii 2.0 vs. Laravel”

  1. An interesting read. When I started looking for a decent framework my two finalists were Symfony2 & Yii1. I went with Symfony first, creating the almost obligated blog, then started playing with Yii. I must say Symfony isn’t exactly friendly when you start with, but then you end creating beautifuly organized bundles, Doctrine was great being used to Hibernate, and Twig templating does a great job, really. So when I tried Yii1 I really missed that big architecture and I continued with S2 and forgeting about Yii.

    A couple days ago I started using Laravel as a lot of people are talking about it. Installation was a breeze and I started coding in it without any problem right after pulling it with Composer, nice! Easy to use, right to the point etc. However some stuff in it is not exaclty artesanal. Defining entity relations in models? ‘Magic’ facades making code look pretty at the cost of braking code navigation. The blade templating system is very basic and its syntax isn’t intuitive nor cool (in fact Laravel should have taken Twig too). Basic stuff like prefixing table names was broken (4.2.3) as it relies in incomplete ‘magic’ methods taking tables from class names/files names/I dont know and error stacktraces looks great but aren’t that useful.

    Laravel so popular because is very easy to start playing with (basically the same PHP phenomenon), even if you haven’t used a framework before, and you can do some amazing stuff with, as it does a great good job getting out of the way letting you obtain something useful without losing time, but it’s far from Symfony’s elegance. I’m waiting til Yii2 becomes stable to start playing with it and hopefuly get amazed with a framework that works out of the box like Laravel, but with a more serious implementation. Don’t get me wrong, Laravel is very cool no dobut about it, but defeinitly is not the holy grail.

    1. Please put this comment in perspective after reading the update at the top of the post. Obviously, my opinion has shifted towards laravel. Here is the originial comment:

      If you try Yii2, I think you will lose interest in the other frameworks. The one downside to Yii2 is the documentaion at this point, but they are working on it. Also, some books on Yii2 will be released soon.

      I always felt that Symfony went in too many directions, Doctrine, Yaml, twig, etc. The advanced template for Yii2 gives you a great starting point for building your own custom template and that is what I’m working on now. Absolutely loving it…

  2. Do you really think that YII2 will win it’s niche among other frameworks?
    They were creating Yii2 too long. What if they lost their chance?
    Laravel has the same features and much more really working extensions, and a lot of community.
    Well in our ex-USSR countries it is still a popular framework.
    But what about english speaking countries?

  3. Thank you for your comment. Yes, I think Yii 2 will become more popular than Laravel, that is my prediction. I don’t think that Laravel is a bad framework, I just believe that Yii 2 offers more. There are many differences between the 2 frameworks and Laravel is getting ready to release V5, so the landscape will shift again. Ultimately developers will use the framework they are most comfortable with, and many who try Yii 2 will fall in love with it as I did.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s