Laravel vs Symfony

This is a question that frequently arises. Someone will ask me “Oh, you [work with/prefer/love] Laravel?” and my answer to that is a simple yes. What baffles me is the very frequent follow up question to that, which is “Well, what about Symfony?”. Every time I get this question, I’m stricken by it because I have never understood the question. It seems like there is a battle, Laravel vs Symfony.

When someone ask me that question my brain tries to figure out why this person is asking me that. Is it because the person prefers Symfony2 over Laravel? Is it because they think that they are mutually exclusive? That somehow you have to make a choice between the two?

A moot question

Let me explain why I don’t really consider this a legit question. As of todays’ version of the Laravel framework package which is 4.1.28 as I am writing this, it has 24 dependencies. Out of these, 12 are Symfony2 components. So you could easily say that Laravel is depending a lot on those components.

That is actually a great philosophy for the framework. Since the components it’s depending on are well written, do one job great each and are fully tested. This means Laravel can leave the lower level heavy lifting to those components. They handle HTTP requests and responses, routing, filesystem operations and the artisan command line tool, just to name a few.

This lets Laravel focus on wrapping/extending the components and providing a more elegant interface for the developer to work with. And the underlying components are always easy to get if you need to do some lower level work. Try this:

<?php
$requestInstance = Request::instance();

This will return an instance of Illuminate\Http\Request. If we take a look at that class we can see that it extends the Symfony component

<?php
class Request extends \Symfony\Component\HttpFoundation\Request

This is a great example of how Laravel (the Illuminate namespace) is wrapping/extending the functionality of one of the components.

Let’s stop trying to compare Laravel vs Symfony

I think that they really can’t be compared on an equal level. Let’s just embrace the synergy that Laravel accomplishes with Symfony2 as a foundation and then adding its sugar on top. This is one of the things that I think makes it so great.

  • Pablo Ezequiel Leone Signetti

    I don’t understand your point. Are you saying that Laravel is a symfony extension?

    Laravel use a part of the symfony components, not all and the architecture is different, easier.

    I prefer Laravel, and I think we can compare them because they are used for the same task and whether you know them or not, Laravel is easier to understand, better documented, and faster for development.

    Nice post!

  • @niklasmodess:disqus Great article !

    When someone ask Symfony2 vs Laravel, I think it means they want to know which one is suitable for their web app. In my perspective Symfony2 is vast and it’s suitable to develop big web system not just website. I never checked Laraval yet!

    Let’s just rephrase the question? I want to develop the web tool (Saas). I am worried about code standard, security, extendibility and so. Which one will you pick Symfony2 or Laravel and why?

    • This is a fairly better question, but I’m still not very fond of it. Since Laravel is only a layer on top of Symfony2 (pretty much), where all the underlying components are easily accessible, you get the extendability of Symfony2. So if you can extend a component in Symfony2, you can do the same with it in Laravel.

      Security and performance might be one thing, but both have extremely active developer communities and it will probably be hard to tell which one is doing it better. It will mostly come down to a matter of personal taste.

  • turpana

    So you prefer Lavarel, but what’s that sugar on top that you refer to? I get that Lavarel is built with Symfony2 components, but I’m still interested as to why I might try Lavarel vs Symfony2 on its own. FWIW, I’m coming from the perspective of looking for a light php framework. I usually build with Drupal or WordPress, but am looking to try something new for a project that doesn’t require the CMS component.

    • Let’s take routing for example. Laravel will bootstrap the necessary components and provides routes.php out of the box with a lot of shortcuts to creating routes.

      Here’s how you would do it in Symfony:

      
          use SymfonyComponentRoutingRouteCollection;
          use SymfonyComponentRoutingRoute;
      
          $collection = new RouteCollection();
          $collection->add('_welcome', new Route('/', array(
              '_controller' => 'AcmeDemoBundle:Main:homepage',
          )));
      
          return $collection;
      

      In Laravel that would be something like this:

      
          Route::get('/', [email protected]');
      

      You can also simply run closures on routes in Laravel:

      
          Route::get('/', function () {
              return 'Hello world!';
          });
      
      • turpana

        Cool, thanks for the example.

      • dipsik

        what about yaml and annotations?
        use SensioBundleFrameworkExtraBundleConfigurationRoute;
        /**
        * @Route(“/item/{id}”, requirements={“id” = “[0-9]+”})
        */
        public function getItem($id) {}

        • I’m not a fan of either YAML or annotations for routes. With YAML you can’t use a closure for simple and fast routes. Personally I also use dummy routes a lot for debugging purposes. With annotations you do not have your routes gathered in one place, I like the idea of having them all in routes.php.

          This is of course my personal preference and I’m not saying either is better than the other. The routes example was just a point to show how Laravel often adds that extra layer that makes it easier to work with.

          • > I’m not a fan of either YAML or annotations for routes. With YAML you can’t use a closure for simple and fast routes. Personally I also use dummy routes a lot for debugging purposes.

            You use the wrong tool for debugging purposes. And yes, mixing controllers with closures leads to a muddy code.

            > With annotations you do not have your routes gathered in one place, I like the idea of having them all in routes.php.

            This looks convenient for you just because you put everything in one place. If you developed a huge application with more than one module you’d suddenly notice that not all of your routes are in one place, because they go along with a module they belong to. And then have routes in PHPdoc blocks in controllers is not a bad idea at all.

          • When I need to do some minor debugging, I just use a route even though it’s quick and dirty.

            I have been a developer on a few huge applications. I still think that each module should have their routes gathered in one place in each module.

            But again, it’s what I find prefer and find convenient. Just like pretty much anything, it won’t work in all cases.

          • How would you share your module if routes don’t go along with it? How would you install 3d-party module without routes? Look at Symfony bundles and how they are packed.

            Regarding debugging. Look at Symfony debug toolbar. It contains a lot of information (request, form data, template variables, db queries and other), it may intercept redirects, so you know what’s going on after successful form submissions.

          • You misunderstood me. I meant that each module can have their own routes in one place (file).

          • Well, routes in controller annotations is also a “one place”. Controllers are API ends of an application, and information on how to reach them is located right in place.

  • Teoh Han Hui

    An apples-to-apples comparison should be between Laravel and the Symfony2 framework, not the Symfony components. The Symfony routes example in your comment is totally not how it’s usually done.

    I am a developer who’s been working with (i.e. using) the Symfony2 framework for a year and a half now…

    • I got that from the current Symfony2 documentation. Could you show how it’s done in a correct way?

      • Teoh Han Hui

        http://symfony.com/doc/current/book/routing.html

        Oops… I just realized you referred to the PHP code snippets. I’d think most devs would go with YAML and annotations, much like how most devs would go with Twig templating instead of PHP templating (in the context of Symfony2 framework).

        So it actually becomes just a matter of configuration (Symfony relies on convention in a lot of places, but not for specifying routes). No code required.

  • Nice article, and I too am guilty of asking that question to my developer friends that use Laravel.

    The reason I ask is because I’ve had some exposure to Symfony, but not Laravel (not since v2 at least); so I am genuinely interested in some of the differences in actually using one vs the other… what happens when you have to get your hands dirty and build an app! 😀

    I am often told that Laravel’s learning curve is much easier to follow, and a lot of things just work intuitively (I’m told you can practically guess how something should be done, and it often works), and it really keeps things simple.

    Symfony on the other hand is (can be) quite complex, and the learning curve is incredibly steep. I gravitated towards it because of the architectural choices made in the framework, the configurability and service oriented nature of Symfony.

    While not necessarily simple or intuitive, once you get over the initial learning curve, Symfony allows you to create very complex and high quality apps in a very short time (relatively speaking).

    Back to the question of Laravel and Symfony going together; I think the underlying question is: Is Laravel a valid alternative to Symfony for large scale or complex (enterprise) apps? How about medium scale apps?

    (because Symfony can get seriously confusing, the learning curve is terrible)

    • I agree with pretty much everything you say, I wouldn’t really say that Symfony’s learning curve is incredibly steep though. But it is a bit steeper than Laravels.

      Laravel and Symfony go very well together! And if you don’t believe me, read this reply by Taylor Otwell, creator or Laravel, on a reddit discussion on the topic of Laravel vs Symfony: http://www.reddit.com/r/PHP/comments/1bdln1/symfony2_vs_laravel/c96ohf1

      When it comes to choosing between the two in applications on a larger scale I wouldn’t say there’s that big of a difference. Laravel will perform slightly worse in benchmarks but the underlying concepts are pretty much the same. Configurability is pretty much the same, and Laravel provides an awesome service oriented nature (service providers).

      All in all: you should go with what you feel more comfortable with.

      • That’s a good discussion on Reddit, thanks for the link.

        It’s true that Symfony is not too difficult to get started with, but exposing yourself to all of it’s features, DIC, building bundles, setting up interfaces to decouple inter-bundle dependancies, and the fact that things can be configured in so many different ways, it certainly makes things more difficult. 😀
        (but I digress)

        I may just try to tackle my next small project in Laravel, it really does look nice!

  • Alex

    A moot post