What are the best PHP Frameworks in 2018? Symfony? Laravel? Or else ?? Let’s compare those two!

综合编程 2018-05-18 阅读原文

In this post, I will try to compare most popular PHP frameworks. I know that writing posts like this could be like putting a stick in an anthill, but I will do my best to be fair for both sides

When you start to learn PHP or are deciding about technology stacks for new project, sooner or later you have to make a decision and choose a right PHP framework.

The decision could be really hard because of multiple possibilities. Choosing the right framework could be crucial for your project but also for your PHP learning journey.

The Possibilities

Let's start from the possibilities. In the last of couple years, when you ask programmers about PHP frameworks, the standard answer was always Symfony. Most modern open source software was also rewritten to use more or fewer features from Symfony frameworks. But in last two years, we can observe the growing popularity of Laravel framework.

What are the other options? Well, let's focus only on those most popular ones. Besides Symfony and Laravel, we can also use Codeigniter, Zend, CakePHP, YII, Phalcon and more..

Let's see how popularity has changed in the last two years:

Source: https://trends.google.com/trends/explore?q=laravel,zend,codeigniter,symfony,cakephp

From this chart, we can see that there was Laravel and for a long long time, nothing.

On the second place is almost exactly Symfony and Codeigniter.

Let's look also at the number of questions regarding those frameworks on Stack Overflow:

I am a fan of Symfony, however, I can understand the growing popularity of Laravel Frameworks. For the purpose of this article, I will focus myself on those two frameworks only.

In my next article, I will compare other PHP frameworks.

It's all about complexity!

When you start to learn PHP and you get to the moment when it's time to learn first framework, Laravel will be the better choice for you. Why? Because it's very simple to learn.

Symfony is waaaay more complicated. But when you are an experienced programmer and about to choose technology for a new project, then your decision could be different.

If you are skilled at PHP PRO, then you simply know that Symfony rocks! The possibilities of customization are unlimited. Let's compare this to real life.

Using Laravel is like buying a laptop, and using Symfony is like building a PC from scratch, deciding what parts and components to use. Symfony is way more complexed and perhaps harder to learn. When you master it, you can see it's also way more flexible.

But is it always the best framework to use?

It all depends on the project..

When you have small projects, or are in the first phase of, for example, a startup product, Laravel could be better choice for you. It provides a simple app that you can grow very rapidly. Also, many programmers know Laravel very well so it's going to be easier to scale the project and provide more developers.

But if you are working on a more advanced project, Symfony is the best choice you can make. It's fast, flexible, expandable, stable, and sustainable. Like with Laravel, there are a lot of programmers who know Symfony and, also, there's a huge library of bundles and documentations available online.

Let's compare!

There's one more fact I must point out. Laravel was built on Symfony. Yes, core components like routing, HTTP, sessions and etc. are used from Symfony:

Laravel constructors take what's bests from Symfony and make it simplier and more rapid to use.

Pro's and Con's

Laravel Pro's:

  • Rapid development
  • Simple file and code organization
  • Great documentation
  • ORM
  • Unit testing
  • Many built in functionality
  • Artisan – great Command Line Interface
  • Solid encryption
  • Proper cache with good cleaning

Laravel Con's:

  • Too much queries to DB
  • Problems with working on shared hostings
  • Sometimes it's just slower than other frameworks

Symfony Pro's:

  • Performance, performance, performance!! (in most recent versions using PHP7!)
  • Flexibility
  • Lots of bundles, packages, commercial third party software, documentations etc.
  • ORM
  • Unit testing
  • Many extension projects for CMS like EasyAdmin, SonataProject etc.
  • Symfony profiler – great tool

Symfony Con's:

  • Complex documentation
  • Many differences between maintained versions
  • Steep learning curve

Differences in code

Now, let me show you some sample codes in Symfony and Laravel doing the same, so you can decide what suits you best.

For my examples, I will be using mostly Symfony in version 3.4. There are some differences in Symfony 4.

  1. Installation!

    In Symfony 4, you may install projects through composer. Simply use:

composer create-project symfony/website-skeleton my-project

But in the previous version of Symfony, you need to do more:

sudo mkdir -p /usr/local/bin
sudo curl -LsS https://symfony.com/installer -o /usr/local/bin/symfony
sudo chmod a+x /usr/local/bin/symfony

In Laravel, it is as simply as in Symfony 4:

composer global require "laravel/installer"
laravel new laravel_project
  1. Configuration

The first thing you will probably edit after the installing project is the configuration. In Symfony, there are many configuration files but the most important is in app/config/config.yml . Symfony allows you also to create different configurations for every environment by simply using config_ENV.yml file.

Standard configuration in Symfony 3.4:

# app/config/config.yml
imports:
    - { resource: parameters.yml }
    - { resource: security.yml }
    - { resource: services.yml }

framework:
    secret:          '%secret%'
    router:          { resource: '%kernel.project_dir%/app/config/routing.yml' }
    # ...

# Twig Configuration
twig:
    debug:            '%kernel.debug%'
    strict_variables: '%kernel.debug%'

# ...

In Laravel, you have a file with keys and values .env and a app.php file with standard PHP configuration.

Standard Laravel .env file:

APP_NAME=Laravel
APP_ENV=local
APP_KEY=
APP_DEBUG=true
APP_URL=http://localhost

LOG_CHANNEL=stack

DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=homestead
DB_USERNAME=homestead
DB_PASSWORD=secret

BROADCAST_DRIVER=log
CACHE_DRIVER=file
SESSION_DRIVER=file
SESSION_LIFETIME=120
QUEUE_DRIVER=sync

REDIS_HOST=127.0.0.1
REDIS_PASSWORD=null
REDIS_PORT=6379

MAIL_DRIVER=smtp
MAIL_HOST=smtp.mailtrap.io
MAIL_PORT=2525
MAIL_USERNAME=null
MAIL_PASSWORD=null
MAIL_ENCRYPTION=null

Standard Laravel app.php file:

 'Laravel',
    'env'      => env('APP_ENV', 'production'),
    'debug'    => env('APP_DEBUG', false),
    'url'      => env('APP_URL', 'http://localhost'),
    'timezone' => 'UTC',
    'locale'   => 'en',
    'key' => env('APP_KEY'),
    'providers' => [
    // …
    ],
    'aliases' => [
    // …
    ],
// …
];

For me, Symfony's YAML is more readable. However, if you don't know YAML, then in Laravel, you can live with that

  1. Routing

In Symfony, we mostly define routing by YAML annotations in controller. Example in Symfony 4:

// src/Controller/BlogController.php
namespace AppController;

use SymfonyBundleFrameworkBundleControllerController;
use SymfonyComponentRoutingAnnotationRoute;

class TasksController extends Controller
{
    /**
     * Matches /tasks exactly
     *
     * @Route("/tasks", name="tasks_list")
     */
    public function list()
    {
        // ...
    }

    /**
     * Matches /tasks/*
     *
     * @Route("/tasks/{slug}", name="task_show")
     */
    public function show($slug)
    {
        // $slug will equal the dynamic part of the URL
        // e.g. at /tasks/task1, then $slug='task1'

        // ...
    }
}

In Laravel, we have default route files. That's the place where we define some routes. For example:

Route::get('/', function () {
    return view('welcome', [
        'name' => 'Michal'
    ]);
});


Route::get('/about', function() {
    return view('about');
});

Route::get('/tasks', function() {

    $tasksDB = Task::all();

   
    return view('tasks.index', compact('tasks', 'tasksDB'));
});

As again, you may have noticed that Laravel's routing is in one file and it may be easier (especially for smaller projects).

  1. Templates

Symfony uses Twig templates. It's a very good templating language where you can code front-end in readable templates that are more friendly to front-end developers, etc. Front-end developers may code templates in TWIG without knowledge of PHP.

Little example:


    
        Welcome to Symfony!
    
    
        

{{ page_title }}

In Laravel, we have a PHP based blade templating system. Again, if you know PHP, you don't have to know Twig, but if you are a front-end developer, you need to have some PHP background to code templates in Blade.

Example below:

getLocale() }}">
    
        
        
        
        Laravel
    
    
 

        

TASKS:

Which framework is better?

Personally, I think that there is no straight answer to this question. If you are working in Symfony, and you think that it's a lot better than Laravel or another way, and you are an experienced Laravel developer and you always saysthat Symfony sucks, then all I can say is that you did not understand the framework you're complaining about.

Both frameworks are great – all you need is to know which one will be better for your current project.

So what framework should you use?

In my opinion, you should pick the framework based on the project specification rather than the popularity of the framework.

As a programmer, I've working with Symfony for over five years. Since year one, I'm using also Laravel and I see the pros and cons in both of those frameworks.

The decision is up to you. I recommend you to get familiar with both of them. Then, do some projects using Laravel and some other projects using Symfony. Only experience can give you enough knowledge to choose what framework will be best for your next project.

Looking on global trends, I guess that if you want to call yourself a professional back-end developer, you need to know at least those two frameworks.

Codementor Tutorials

责编内容by:Codementor Tutorials阅读原文】。感谢您的支持!

您可能感兴趣的

进阶系列 —— 使用 Artisan 构建强大的控制台应用... 简介 Artisan 是 Laravel 自带的命令行接口,它为我们在开发过程中提供了很多有用的命令。想要查看所有可用的 Artisan 命令,可使用 list 命令: php artisan list 每个命令...
安全系列 —— 重置密码 简介 想要快速实现该功能?只需要在新安装的 Laravel 应用下运行 php artisan make:auth (如果你已经执行过此命令,可忽略),然后在浏览器中访问 http://your-app.test/register 或...
Laravel Excel 3.0 Laravel Excel is a package by Maatwebsite that makes working with spreadsheets easy. It’s been out since 2013 and just r...
Laravel 5.5 FormRequest 自定义表单请求验证类... 1、把表单验证逻辑写在Controller中,这是最基础的方法,但是不好维护,如: 1 namespace AppHttpControllersAdmin; 2 3 use IlluminateHttpRequest; ...
进阶系列 —— 邮件 简介 Laravel 基于 SwiftMailer 库提供了一套干净、清爽的邮件 API。Laravel 为 SMTP、Mailgun、SparkPost、Amazon SES、PHP 的 mail 函数,以及 sendmail 提供...