On Github tautvilas / laravel-vs-symfony-lecture
Created by Tautvilas Mečinskas
Powered by reveal.js
2013 10
Laravel4:
composer create-project laravel/laravel --prefer-dist
Symfony2:
composer create-project symfony/framework-standard-edition path/ 2.3.5
return array( 'default' => 'mysql', 'connections' => array( 'mysql' => array( 'driver' => 'mysql', 'host' => 'localhost', 'database' => 'database', 'username' => 'root', 'password' => '', ), ), );
Default: Yaml
parameters: database_driver: pdo_mysql database_host: 127.0.0.1 database_port: null database_name: symfony database_user: root
Simple routes
Route::get('user/{name?}', function($name = null) { return $name; });
Route::get('user/profile', array('uses' => 'UserController@showProfile'));
RESTful Controller
class UserController extends BaseController { public function getIndex() { // } public function postProfile() { // } }
Route::controller('users', 'UserController');
There are many ways...
YAML:
contact_process: path: /contact defaults: { _controller: AcmeDemoBundle:Main:contactProcess } methods: [POST]
Annotations:
/** * @Route("/", defaults={"id" = 1}) * @Route("/{id}") */ public function showAction($id) { }
if (Auth::attempt(array('email' => $email, 'password' => $password))) { return Redirect::intended('dashboard'); }
Route::get('profile', array('before' => 'auth', function() { // Only authenticated users may enter... }));
No authorization (roles)
It is possible to use twig inside Laravel4 using TwigBridge
It is possible to hook in other templating engines to Symfony2
Ideal view will look like this:
{# src/Acme/TaskBundle/Resources/views/Default/new.html.twig #} {% form_theme form 'AcmeTaskBundle:Form:fields.html.twig' %} {{ form(form) }}
Ideal controller will look like this:
public function newAction(Request $request) { $task = new Task(); $form = $this->createForm(new TaskType(), $task); $form->handleRequest($request); if ($form->isValid()) { $this->getEntityManager()->persist($task); $this->getEntityManager()->fulsh(); return $this->redirect($this->generateUrl('task_success')); } return array('form'=>form->createView()); }
But it's not easy to get there...
Homebrew solution:
Easy to learn
Fallback to PDO queries is not painful
3rd party libraries Doctrine or Propel:
Laravel4 is a little faster than Symfony2
Benchmark by TechEmpower. Responses per second at 20 queries per request, Linux on i7
Laravel feels much faster (no app_dev.php)
$name = Input::get('name', 'Sally');
$name = Input::get('name', 'Sally');
public function getIndex() { Event::fire('foo', array('name' => 'Dayle')); return 'All done!'; }
public function testGetIndex() { Event::shouldReceive('fire')->once()->with('foo', array('name' => 'Dayle')); $this->call('GET', '/'); }
$name = $this->getRequest()->get('name', 'Sally');
namespace CompanyX\UserBundle\Controller; use FOS\UserBundle\Controller\RegistrationController as BaseController; use FOS\UserBundle\Entity\UserManager; use FOS\UserBundle\Form\Model\CheckPassword; use FOS\UserBundle\Model\UserInterface; use FOS\UserBundle\Model\UserManagerInterface; use Imagine\Filter\Transformation; use Imagine\Gd\Imagine; use Imagine\Image\Box; use CompanyX\MainBundle\Form\DataTransformer\StringToNumberTransformer; use CompanyX\MainBundle\MpController; use CompanyX\ReportBundle\ReportGenerator; use CompanyX\UserBundle\Entity\RoleType; use CompanyX\UserBundle\Entity\User; use CompanyX\UserBundle\Entity\UserPeriod; use CompanyX\UserBundle\Entity\UserSalary; use CompanyX\UserBundle\Entity\UserSort; use CompanyX\UserBundle\Form\Type\CredentialsForm; use CompanyX\UserBundle\Form\Type\UserFormType; use CompanyX\UserBundle\Form\Type\UserSalaryType; use CompanyX\WorkBundle\DateUtils; use PDOException; use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route; use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template; use stdClass; use Symfony\Bundle\FrameworkBundle\Controller\Controller; use Symfony\Component\Form\Exception\TransformationFailedException; use Symfony\Component\Form\FormError; use Symfony\Component\HttpFoundation\RedirectResponse; use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Response; use Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken; use Symfony\Component\Security\Core\Exception\AccessDeniedException;