Today

We'll not speak about SILEX

Symfony CRUD in 10 minutes

How to build a web app writing only a single line of php code

Create a Sandbox

$ composer create-project symfony/framework-standard-edition newProject

Create your layout

<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
        <title>{% block title 'My admin' %}</title>
        <link rel="shortcut icon" href="{{ asset('favicon.ico') }}" />
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        {% block stylesheets %}
            <link href="//netdna.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap.min.css"
                  rel="stylesheet">
            <link href="//netdna.bootstrapcdn.com/font-awesome/4.2.0/css/font-awesome.css"
                  rel="stylesheet">
        {% endblock %}
    </head>
    <body>
        <nav class="navbar navbar-fixed-top">
            <!-- put your nav bar here -->
        </nav>
        <div class="container">
            {% block body '' %}
        </div>
        {% block javascripts %}
            <script src="//code.jquery.com/jquery-2.1.1.js"></script>
            <script src="//netdna.bootstrapcdn.com/bootstrap/3.2.0/js/bootstrap.min.js">
            </script>
        {% endblock %}
    </body>
</html>

Mentally thank the President

Add PUGXGeneratorBundle as a dependency

$ composer require pugx/generator-bundle:2.4.* --dev

First line of code: load the bundle into the symfony kernel

<?php
// app/AppKernel.php

public function registerBundles()
{
    // ...
    if (in_array($this->getEnvironment(), array('dev', 'test'))) {
        // ...
        $bundles[] = new PUGX\GeneratorBundle\PUGXGeneratorBundle();
    }
}

N.B. These steps are the same for every application! So you can prepare a repo containing everything and start there next time!

Create your first Bundle

$ app/console g:b
$ app/console generate:bundle

but I'm lazy, so

Download the symfony 2.6 Bootstrap form

$ wget https://raw.githubusercontent.com/symfony/symfony/master/src/Symfony/Bridge/Twig/Resources/views/Form/bootstrap_3_layout.html.twig

$ mkdir src/Acme/CRUDBundle/Resources/views/Form

$ mv bootstrap_3_layout.html.twig src/Acme/CRUDBundle/Resources/views/Form/bootstrap_3_layout.html.twig

Generate an Entity

$ app/console g:d:entity

Create the shema

$ app/console doc:schema:create

Finally generate the CRUD

$ php app/console pugx:generate:crud                           \
      --entity=AcmeCRUDBundle:Post                             \
      --layout=::base.html.twig                                \
      --theme=AcmeCRUDBundle:Form:bootstrap_3_layout.html.twig \
      --with-write
$ app/console server:run

Questions?

Wait, I know... And What about SILEX?!?

Questions?

Wait, wait... I Know! Why you hate CRUD?!?

I don't hate the CRUD...

BUT

...the CRUD hates me

  • Entities are not only data containers

  • Rarely use cases are aligned with the CRUD of a single entity, but usually involves several different entities

  • Handle and dispose domain objects in the presentation layer does not allow us to protect the invariants

  • Every time you write a CRUD, god kills a kitten

It's your choice!

Silex is a (Fat) microframework

Framework : Service = MicroFramework : MicroService

  • Persistence
  • Templating
  • Form handling

CRUD  Requires

It's your choice!

Symfony CRUD in 10 minutes

By Claudio D'Alicandro

Symfony CRUD in 10 minutes

How to build a web app writing only a single line of php code

  • 2,739