Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Silex versus Symfony, Microframework vs full-stack

Michael C.
November 16, 2016

Silex versus Symfony, Microframework vs full-stack

Michael C.

November 16, 2016
Tweet

More Decks by Michael C.

Other Decks in Programming

Transcript

  1. ME?

  2. SYMFONY CONTROLLER <?php namespace Acme\HelloBundle\Controller; use Symfony\Component\HttpFoundation\Response; class HelloController {

    public function indexAction($name) { return new Response('<html><body>Hello '.$name.'!</body></html>'); } }
  3. SYMFONY CONFIGURATION <?php public function registerBundles() { $bundles = array(

    // ... new Acme\HelloBundle\AcmeHelloBundle(), ); return $bundles; } php app/console generate:bundle --namespace=Acme/HelloBundle --format=yml _hello: pattern: /hello/{name} defaults: { _controller: AcmeHelloBundle:Hello:index }
  4. BENCHMARKING VMWare Fusion: Ubuntu 64-bit Server 15.04
 NGINX
 PHP 5.6.4


    2GB RAM
 4 cores (Intel Core i7, 3.4 GHz). Requests per second:
 551.59 Time per request:
 1.813ms Requests per second:
 1424.30 Time per request:
 0.702ms 0 375 750 1125 1500 SYMFONY SILEX
  5. BENCHMARKING Source: Igor Wiedler Memory Usage:
 931K Wall time:
 6.124ms

    Memory Usage:
 579K Wall time:
 0.86ms Memory Usage:
 738K Wall time:
 0.495ms Dumped
  6. BENCHMARKING 0 250 500 750 1000 SYMFONY SYMFONY
 (DUMPED) PIMPLE

    0 1.75 3.5 5.25 7 SYMFONY SYMFONY
 (DUMPED) PIMPLE Memory (K) Wall time (ms)
  7. BENCHMARKING 0 200 400 600 800 SYMFONY
 (DUMPED) PIMPLE 0

    0.225 0.45 0.675 0.9 SYMFONY
 (DUMPED) PIMPLE Memory (K) Wall time (ms)
  8. FRONT CONTROLLER <?php require_once __DIR__.'/../vendor/autoload.php'; use Silex\Application; use Michaelcullum\Router; include

    __DIR__.'/config.php'; // Basic App Setup Stuff $app = new Application(); $router = new Router($app); // Register service providers include __DIR__.'/../src/Michaelcullum/registerProviders.php'; // Set twig layout template $app->before(function () use ($app) { $app['twig']->addGlobal('layout', $app['twig']->loadTemplate('base.html.twig')); }); $app->mount('/', $router->setBasicRoutes()); $app->run();
  9. CONTROLLERS <?php namespace Michaelcullum\Controller; use Silex\Application; use Symfony\Component\HttpFoundation\Request; use Symfony\Component\Validator\Constraints

    as Assert; class FooController { public function contactAction(Request $request, Application $app) { return $app['twig']->render('contact.html.twig', []); } }
  10. FORMS $form = $app['form.factory']->createBuilder('form') ->add('name', 'text', array( 'required' => true,

    'constraints' => array(new Assert\NotBlank(), new Assert\Length(array('min' => 5))) )) ->getForm(); if ('POST' == $request->getMethod()) { $form->bind($request); if ($form->isValid()) { $data = $form->getData(); return $app['twig']->render('contact.html.twig', []); } }
  11. ROUTING <?php namespace Michaelcullum; use Silex\Application; use Symfony\Component\HttpFoundation\Request; class Router

    { public $app; function __construct(Application $app) { $this->app = $app; } public function setBasicRoutes() { $controllerFactory = $this->app['controllers_factory']; $controllerFactory->get('/', 'Michaelcullum\Controller\BasicPages::indexAction'); $controllerFactory->get('/who', 'Michaelcullum\Controller\BasicPages::whoAction'); return $controllerFactory; } }
  12. PROVIDERS <?php use Silex\Provider\FormServiceProvider; use Silex\Provider\TwigServiceProvider; use Silex\Provider\DoctrineServiceProvider; use Symfony\Component\Validator\Constraints

    as Assert; use Silex\Provider\ValidatorServiceProvider; use Silex\Provider\TranslationServiceProvider; $app->register(new FormServiceProvider()); $app->register(new ValidatorServiceProvider()); $app->register(new TranslationServiceProvider(), array( 'translator.messages' => array(), )); $app->register(new TwigServiceProvider(), array( 'twig.path' => __DIR__.'/../../views', )); $app->register(new DoctrineServiceProvider(), array( 'dbs.options' => array(array( 'driver' => 'pdo_mysql', 'host' => 'localhost', 'dbname' => getenv('DB_NAME'), 'user' => getenv('DB_USER'), 'password' => getenv('DB_PASSWORD'), 'charset' => 'utf8', )), ));