Symfony2

Powerful and flexible web application framework in state of the art PHP





werbmardi Fribourg, 3.7.2012 © David Buchmann, Liip AG


CMS versus framework



or


customize versus build

Symfony2

Usage

Dependeny Injection

Dependeny Injection

class MyClass
{
    public function do()
    {
        global $variable;
        echo $variable;
    }
}
            

NO!

Dependeny Injection

class MyClass
{
    public function do()
    {
        echo Registry::instance()->get('variable');
    }
}
            

Meh. A bit better but not good.

Dependeny Injection

class MyClass
{
    private $variable;
    public function __construct($variable)
    {
        $this->variable = $variable;
    }
    public function do()
    {
        echo $this->variable;
    }
}
            

This is dependency injection!

Dependeny Injection

The MVC of Symfony

Doctrine ORM




Model

use Doctrine\ORM\Mapping as ORM;

/**
 * @ORM\Table
 */
class License
{
    /**
     * @var integer $id
     *
     * @ORM\Column(name="id", type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    private $id;

    /**
     * @var string $startDate
     *
     * @ORM\Column(name="start_date", type="date")
     */
    private $startDate;
    ...
            
/**
 * @var Vendor the vendor instance of this license
 *
 * @ORM\ManyToOne(targetEntity="Vendor", inversedBy="licenses")
 * @ORM\JoinColumn(name="vendor_id", referencedColumnName="id")
 */
private $vendor;
        
$license = new License($id, new DateTime());
$vendor = new Vendor(...);
$license->setVendor($vendor);
// tell the document manager about our object
$documentManager->persist($license);
// save everything to database (will also save vendor)
$documentManager->flush();
        

Doctrine shell commands

app/console doctrine:schema:update --dump-sql
CREATE TABLE license (id INT NOT NULL, start_date DATE NOT NULL)

app/console doctrine:schema:drop --dump-sql
DROP TABLE license
            

$ app/console doctrine:migrations:status

 == Configuration

>> Name:                   Application Migrations
>> Database Driver:        pdo_pgsql
>> Database Name:          lbe
>> Configuration Source:   manually configured
>> Version Table Name:     migration_versions
>> Migrations Namespace:   Application\Migrations
>> Migrations Directory:   /home/david/symfony/app/Migrations
>> Current Version:        2012-06-29 11:21:38 (20120629112138)
>> Latest Version:         2012-06-29 11:21:38 (20120629112138)
>> Executed Migrations:    1
>> Available Migrations:   1
>> New Migrations:         0
            



Control

Routing: Map request to controller

# app/config/routing.yml
hello:
    pattern:  /hello/{name}
    defaults: { _controller: AcmeHelloBundle:Hello:index }
            

Controller

namespace Acme\HelloBundle\Controller;

use Symfony\Component\HttpFoundation\Response;

class HelloController
{
  public function indexAction($name)
  {
    $navigation = $this->getNavigation();
    return $this->render(
        'AcmeHelloBundle:Hello:index.html.twig',
        array('name' => $name, 'navigation' => $navigation)
    );
  }
}
            



View

Twig templating

<!DOCTYPE html>
<html>
  <head>
    <title>Welcome to Symfony!</title>
  </head>
  <body>
    <h1>Hello: {{ name }}</h1>

    <ul id="navigation">
      {% for item in navigation %}
        <li>
          <a href="{{ item.href }}">{{ item.caption }}</a>
        </li>
      {% endfor %}
    </ul>
  </body>
</html>
            

Assetic: Pain-free asset management

Assetic: Pain-free asset management

{% javascripts output='assets/*.js'
    '../vendor/yui/build/yui/yui-min.js'
    '@SKVCoreBundle/Resources/public/js/main.js'
%}
    <script src="{{ asset_url }}" type="text/javascript"></script>
{% endjavascripts %}
            

Bundles

Reusable code pieces with Symfony2 integration

Composer: Dependency management reloaded

There is more

Thank you!



Questions / Input / Feedback ?



@dbu