Introduction to Symfony2 - NYPHP

Post on 27-Jan-2015

121 views 2 download

Tags:

description

 

Transcript of Introduction to Symfony2 - NYPHP

INTRODUCTION TO

SYMFONY2Juozas Kaziukėnas // juokaz.com // @juokaz

Juozas Kaziukėnas, Lithuanian

You can call me Joe

More info http://juokaz.com

WHAT A FRAMEWORK SHOULD DO?

WHAT A FRAMEWORK SHOULD DO?

•Out of the box functionality• Routing• Security• Best practices• Standardization•Development speed• etc.

WHAT IS SYMFONY2?

WHAT IS SYMFONY2?

• A successor of symfony 1.X• Rewritten from scratch• Turns requests to responses, that’s it• Heavily utilizes PHP 5.3+ features• Component oriented architecture• Supported by Sensio Labs agency, run by Fabien Potencier

COMPONENTS

• Building blocks for the framework

• Like: HTTP kernel, Form, Validation, Security, Event dispatcher, Config, Console, etc.

• Independent self-contained components

• Can be used without using Symfony2 as a web framework

WHAT SYMFONY2 DOES WELL?

WHAT SYMFONY2 DOES WELL?

• Components• Inspired Composer to be created• Silex vs Symfony2 vs your own• Fully working standard distribution• Service container for dependencies

SILEX

require_once __DIR__.'/../vendor/autoload.php';

$app = new Silex\Application();

$app->get('/hello/{name}', function($name) use($app) { return 'Hello '.$app->escape($name); });

$app->run();

COMPOSER

• 3rd party dependency management

•Makes life so much easier

• Allows not to have 3rd party checked in into a master repo

• Project-specific dependencies vs PEAR-like system-wide

•One file composer.json to manage all expectations

• Became a de facto standard in PHP projects

STARTING WITH SYMFONY2

STARTING WITH SYMFONY2

composer.phar create-project symfony/framework-standard-edition path/to/install 2.1.x-dev

php ./app/console server:run

Browse http://localhost:8000/app_dev.php

BUNDLES

• “Modules” to separate application code

• Contains controllers, models, templates, etc.

• Symfony framework itself is a bundle called FrameworkBundle

• Repository of available bundles http://knpbundles.com/

• Can be extended to customize

BUNDLES

// src/Acme/DemoBundle/Controller/WelcomeController.php

namespace Acme\DemoBundle\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;

class WelcomeController extends Controller{ public function indexAction() { return $this->render('AcmeDemoBundle:Welcome:index.html.twig'); }}

SERVICE CONTAINER

// app/config/config.ymlservices: my_mailer: class: Acme\HelloBundle\Mailer arguments: [sendmail]

class WelcomeController extends Controller{ public function sendEmailAction() { $mailer = $this->get('my_mailer'); $mailer->send('ryan@foobar.net', ...); }}

COMMUNITY

COMMUNITY

• Symfony2 on Twitter• Conferences, user groups• All of this?• Good market to be a developer• Less bugs, active GitHub•Drupal

QUESTIONS?

THANKS!Juozas Kaziukėnas

@juokaz