On Github 2dotstwice / aft-php-silex-slides
{ "require": { "silex/silex": "~1.3", "twig/twig": "^1.22" } }
App store for Composer
<?php require_once __DIR__ . '/../vendor/autoload.php'; $app = new Silex\Application(); // Handle requests ... $app->run();
Hello world
GET http://<your-url>/hello
$app->get( '/hello', function (Request $request) { return new Response('Hello world!'); } );
Variable parameters in the URL
GET http://<your-url>/hello/AFT
$app->get( '/hello/{name}', function (Request $request, $name) { return new Response('Hello ' . $name . '!'); } );
HTTP status codes
$app->get( '/blog/{postId}', function (Request $request, $postId) { $posts = [ 1 => 'Just another Silex blog.', 2 => 'My thoughts on PHP.', 5 => 'AFT workshop review.', ]; if (!isset($posts[$postId])) { return new Response('Not found.', 404); } else { return new Response($posts[$postId], 200); } } );
Query parameters
GET http://<your-url>/search?name=...
$app->get('/search', function (Request $request) { $filter = $request->query->get('name'); $names = ['Bill Gates', 'Steve Jobs', 'Steve Wozniak']; $matches = []; foreach ($names as $name) { if (stripos($name, $filter) !== false) { $matches[] = $name; } } return new Response( count($matches) . ' result(s): ' . implode(', ', $matches) ); });
Posting data
POST http://<your-url>/contact
$app->post( '/contact', function (Request $request) { $filename = __DIR__ . '/files/contact-' . time() . '.txt'; $content = $request->getContent(); file_put_contents($filename, $content); return new Response($filename); } );
Perform additional actions on each route...
$app->after(function (Request $request, Response $response) { $response->headers->set('X-Generated-By', 'Silex'); });
... or on specific routes
$app->get(...) ->after( function (Request $request, Response $response) { $response->headers->set('X-Generated-By', 'Silex'); } );
$app->before( function (Request $request) { // Modify $request before it is handled... } );
$app->after( function (Request $request, Response $response) { // Modify $response before it is sent back... } );
$app->finish( function (Request $request, Response $response) { // Additional actions such as logging... // Changes to $request or $response will be ignored here. } );
Write your own service instantiation function ...
$app['twitter'] = $app->share( function() { return new TwitterClient(); } ); // ... $app['twitter']->getTweets(...);
... or use an existing ServiceProvider
$app->register(new Silex\Provider\SessionServiceProvider()); // ... $app['session']->set(...); $app['session']->get(...);
Storing data
POST http://<your-url>/login
$app->register(new Silex\Provider\SessionServiceProvider()); $app->post( '/login', function (Request $request) use ($app) { $username = $request->request->get('username'); $password = $request->request->get('password'); if ($username == 'john.doe' && $password == 'secret') { $app['session']->set('username', $username); return new Response('Logged in.', 200); } else { return new Response('Access denied.', 403); } } );
Retrieving stored data
GET http://<your-url>/user
$app->get( '/user', function (Request $request) use ($app) { $username = $app['session']->get('username'); if (!empty($username)) { return new Response('Logged in as ' . $username); } else { return new Response('Not logged in.'); } } );
Storing data
file_put_contents($filename, $contents);
Retrieving data
$contents = file_get_contents($filename);
Storing data as JSON
$users = [ 'john.doe' => [ 'password' => '662azd', 'bio' => '...', ], 'an0n' => [ 'password' => 'aazf959', 'bio' => '...', ], ]; $json = json_encode($users); file_put_contents('users.json', $json);
Reading JSON data
$json = file_get_contents('users.json'); $users = json_decode($json);
[ 'john.doe' => [ 'password' => '662azd', 'bio' => '...', ], 'an0n' => [ 'password' => 'aazf959', 'bio' => '...', ], ];
$app->register( new Silex\Provider\TwigServiceProvider(), ['twig.path' => __DIR__ . '/../templates'] );
$app->get( 'blog', function(Request $request) use ($app) { $posts = [ 1 => 'Just another Silex blog.', 2 => 'My thoughts on PHP.', 5 => 'AFT workshop review.', ]; $html = $app['twig']->render('blog.twig', [ 'title' => 'My awesome blog.', 'posts' => $posts, ]); return new Response($html); } );
<h1>{{ title }}</h1> <ul> {% for id, post in posts %} <li><a href="/blog/{{ id }}">{{ post }}</a></li> {% endfor %} </ul>