Silex

David Flores

Co-Mantainer at @DrupalConsole, Linux, Drupal, Symfony, Silex, BackEnd, Seguridad, Python, Open Source, Father ...sometimes #Nerd Speaker

david@indava.com

@dmouse

Micro

Full

Framework

Silex

Symfony

Old PHP

<?php

function formulario_ingresar_usuarios($config_user,$sec) {
    $texto = '';                                                               
    $texto .= '<form name="formulario_acceso_vista" id="formulario_acceso_vista" method="post" action="index.php?sec='.$sec.'">';
    $texto .= '<div class="div_error_login" >';
    $error_de_usuario = (isset($_GET["error_usuario"])) ?$_GET["error_usuario"]:'';
    if($error_de_usuario  == 'si')
            { $texto .=  '<span class="error_usuario">'.txt_error_acceso_vista.'</span>'; }
    else
            { $texto .=  ' '; }
    
    // ...
    // bad, bad, bad

}

Old PHP

<?php 

function obtenerSeccion() {
    if(!isset($_GET["sec"]) || $_GET["sec"]=='')
        {$sec = 1;}
    else
        { $sec = $_GET["sec"]; } 
    return (int)$sec;                        
}

Everytime you use GLOBALS god kills a kitten

Modern PHP

Namespaces

<?php

use Silex\Application;
# Directory

silex
  - src
    - Application.php

Anonymous Function

<?php


$callBack = function ($var1, $var2) {
  // Do something.
}

$callBack("a", "b");

// Silex
$app->get("/", function() {
  // Do something.
});

// Closure
$app->get("/", function() use ($app) {
  return $app['twig']->render('index.html.twig');
});

Autoload

<?php

function autoloadModel($className) {
    $filename = "models/" . $className . ".php";
    if (is_readable($filename)) {
        require $filename;
    }
}

function autoloadController($className) {
    $filename = "controllers/" . $className . ".php";
    if (is_readable($filename)) {
        require $filename;
    }
}

spl_autoload_register("autoloadModel");
spl_autoload_register("autoloadController");

Silex

<?php

$app->get("/", function() {

  return "hello world";
});

$app->post("/register", function(Request $request) {

  $email = $request->request->get("email");
  // save email.

  return "Your record has been saved.";
});

$app->match("/login", function() {
  // Login method.
})
->methods("GET|POST");

Demo

https://slides.com/dmouse/silex-2016

Made with Slides.com