keeping it small: getting to know the slim micro framework

135
Keeping it small Getting to know the Slim micro framework @JeremyKendall

Upload: jeremy-kendall

Post on 18-May-2015

1.118 views

Category:

Technology


1 download

TRANSCRIPT

Page 1: Keeping it Small: Getting to know the Slim Micro Framework

Keeping it smallGetting to know the Slim micro framework

@JeremyKendall

Page 2: Keeping it Small: Getting to know the Slim Micro Framework

Jeremy Kendall

Page 3: Keeping it Small: Getting to know the Slim Micro Framework

Jeremy Kendall

I love to code

Page 4: Keeping it Small: Getting to know the Slim Micro Framework

Jeremy Kendall

I love to code

I’m terribly forgetful

Page 5: Keeping it Small: Getting to know the Slim Micro Framework

Jeremy Kendall

I love to code

I’m terribly forgetful

I take pictures

Page 6: Keeping it Small: Getting to know the Slim Micro Framework

Jeremy Kendall

I love to code

I’m terribly forgetful

I take pictures

I work at OpenSky

Page 7: Keeping it Small: Getting to know the Slim Micro Framework

Micro framework?

Page 8: Keeping it Small: Getting to know the Slim Micro Framework

Micro framework?

Concise codebase

Page 9: Keeping it Small: Getting to know the Slim Micro Framework

Micro framework?

Concise codebase

Clear codebase

Page 10: Keeping it Small: Getting to know the Slim Micro Framework

Micro framework?

Concise codebase

Clear codebase

Addresses a small set of use cases

Page 11: Keeping it Small: Getting to know the Slim Micro Framework

Micro framework?

Concise codebase

Clear codebase

Addresses a small set of use cases

Addresses those use cases well

Page 12: Keeping it Small: Getting to know the Slim Micro Framework

What is Slim?

Page 13: Keeping it Small: Getting to know the Slim Micro Framework

What is Slim?

Inspired by Sinatra

Page 14: Keeping it Small: Getting to know the Slim Micro Framework

What is Slim?

Inspired by Sinatra

Favors cleanliness over terseness

Page 15: Keeping it Small: Getting to know the Slim Micro Framework

What is Slim?

Inspired by Sinatra

Favors cleanliness over terseness

Favors common cases over edge cases

Page 16: Keeping it Small: Getting to know the Slim Micro Framework

Installing Slim

Page 17: Keeping it Small: Getting to know the Slim Micro Framework

RTFM

Page 18: Keeping it Small: Getting to know the Slim Micro Framework

RTFM ;-)

Page 19: Keeping it Small: Getting to know the Slim Micro Framework

Don’t forget .htaccess!

RewriteEngine OnRewriteCond %{REQUEST_FILENAME} !-fRewriteRule ^ index.php [QSA,L]

http://docs.slimframework.com/pages/routing-url-rewriting/

Page 20: Keeping it Small: Getting to know the Slim Micro Framework

Hello world<?php

require '../vendor/autoload.php';

$app = new \Slim\Slim();

$app->get('/hello/:name', function ($name) { echo "Hello, $name";});

$app->run();

Page 21: Keeping it Small: Getting to know the Slim Micro Framework

Hello world<?php

require '../vendor/autoload.php';

$app = new \Slim\Slim();

$app->get('/hello/:name', function ($name) { echo "Hello, $name";});

$app->run();

Page 22: Keeping it Small: Getting to know the Slim Micro Framework

Hello world<?php

require '../vendor/autoload.php';

$app = new \Slim\Slim();

$app->get('/hello/:name', function ($name) { echo "Hello, $name";});

$app->run();

Page 23: Keeping it Small: Getting to know the Slim Micro Framework

Hello world<?php

require '../vendor/autoload.php';

$app = new \Slim\Slim();

$app->get('/hello/:name', function ($name) { echo "Hello, $name";});

$app->run();

Page 24: Keeping it Small: Getting to know the Slim Micro Framework

Hello world<?php

require '../vendor/autoload.php';

$app = new \Slim\Slim();

$app->get('/hello/:name', function ($name) { echo "Hello, $name";});

$app->run();

Page 25: Keeping it Small: Getting to know the Slim Micro Framework

Let’s look at a Slim application

Page 26: Keeping it Small: Getting to know the Slim Micro Framework

Flaming Archer

Page 27: Keeping it Small: Getting to know the Slim Micro Framework

Flaming Archer

wat

Page 28: Keeping it Small: Getting to know the Slim Micro Framework

“Great repository names are short and memorable. Need inspiration? How about flaming-archer.”

Page 29: Keeping it Small: Getting to know the Slim Micro Framework

Flaming Archer

Page 30: Keeping it Small: Getting to know the Slim Micro Framework

Flaming ArcherPhoto 365 project

Page 31: Keeping it Small: Getting to know the Slim Micro Framework

Flaming ArcherPhoto 365 project

Built in 4 days (Saturday through Tuesday)

Page 32: Keeping it Small: Getting to know the Slim Micro Framework

Flaming ArcherPhoto 365 project

Built in 4 days (Saturday through Tuesday)

Basic application — a few bells, no whistles

Page 33: Keeping it Small: Getting to know the Slim Micro Framework

Flaming ArcherPhoto 365 project

Built in 4 days (Saturday through Tuesday)

Basic application — a few bells, no whistles

Routing

Page 34: Keeping it Small: Getting to know the Slim Micro Framework

Flaming ArcherPhoto 365 project

Built in 4 days (Saturday through Tuesday)

Basic application — a few bells, no whistles

Routing

Twig views

Page 35: Keeping it Small: Getting to know the Slim Micro Framework

Flaming ArcherPhoto 365 project

Built in 4 days (Saturday through Tuesday)

Basic application — a few bells, no whistles

Routing

Twig views

Middleware

Page 36: Keeping it Small: Getting to know the Slim Micro Framework

4 views

Page 37: Keeping it Small: Getting to know the Slim Micro Framework
Page 38: Keeping it Small: Getting to know the Slim Micro Framework
Page 39: Keeping it Small: Getting to know the Slim Micro Framework
Page 40: Keeping it Small: Getting to know the Slim Micro Framework
Page 41: Keeping it Small: Getting to know the Slim Micro Framework

phploc --exclude vendor,tests,templates .

phploc 1.6.4 by Sebastian Bergmann.

Directories: 7Files: 13

Lines of Code (LOC): 876 Cyclomatic Complexity / Lines of Code: 0.04Comment Lines of Code (CLOC): 272Non-Comment Lines of Code (NCLOC): 604

Page 42: Keeping it Small: Getting to know the Slim Micro Framework

Configuration

Page 43: Keeping it Small: Getting to know the Slim Micro Framework

return array( 'slim' => array( 'templates.path' => __DIR__ . '/templates', 'log.level' => 4, 'log.enabled' => true, 'log.writer' => new Slim\Extras\Log\DateTimeFileWriter( array( 'path' => __DIR__ . '/logs', 'name_format' => 'y-m-d' ) ) ), 'twig' => array( // . . . ), 'cookies' => array( // . . . ), 'flickr.api.key' => 'FLICKR API KEY', 'pdo' => array( // . . . ));

Page 44: Keeping it Small: Getting to know the Slim Micro Framework

return array( 'slim' => array( 'templates.path' => __DIR__ . '/templates', 'log.level' => 4, 'log.enabled' => true, 'log.writer' => new Slim\Extras\Log\DateTimeFileWriter( array( 'path' => __DIR__ . '/logs', 'name_format' => 'y-m-d' ) ) ), 'twig' => array( // . . . ), 'cookies' => array( // . . . ), 'flickr.api.key' => 'FLICKR API KEY', 'pdo' => array( // . . . ));

Slim

Page 45: Keeping it Small: Getting to know the Slim Micro Framework

return array( 'slim' => array( 'templates.path' => __DIR__ . '/templates', 'log.level' => 4, 'log.enabled' => true, 'log.writer' => new Slim\Extras\Log\DateTimeFileWriter( array( 'path' => __DIR__ . '/logs', 'name_format' => 'y-m-d' ) ) ), 'twig' => array( // . . . ), 'cookies' => array( // . . . ), 'flickr.api.key' => 'FLICKR API KEY', 'pdo' => array( // . . . ));

Slim

Views

Page 46: Keeping it Small: Getting to know the Slim Micro Framework

return array( 'slim' => array( 'templates.path' => __DIR__ . '/templates', 'log.level' => 4, 'log.enabled' => true, 'log.writer' => new Slim\Extras\Log\DateTimeFileWriter( array( 'path' => __DIR__ . '/logs', 'name_format' => 'y-m-d' ) ) ), 'twig' => array( // . . . ), 'cookies' => array( // . . . ), 'flickr.api.key' => 'FLICKR API KEY', 'pdo' => array( // . . . ));

Slim

Views

Cookies

Page 47: Keeping it Small: Getting to know the Slim Micro Framework

return array( 'slim' => array( 'templates.path' => __DIR__ . '/templates', 'log.level' => 4, 'log.enabled' => true, 'log.writer' => new Slim\Extras\Log\DateTimeFileWriter( array( 'path' => __DIR__ . '/logs', 'name_format' => 'y-m-d' ) ) ), 'twig' => array( // . . . ), 'cookies' => array( // . . . ), 'flickr.api.key' => 'FLICKR API KEY', 'pdo' => array( // . . . ));

Slim

Views

Cookies

My stuff

Page 48: Keeping it Small: Getting to know the Slim Micro Framework

$config = require_once __DIR__ . '/../config.php';

// Prepare app$app = new Slim\Slim($config['slim']);

Page 49: Keeping it Small: Getting to know the Slim Micro Framework

$config = require_once __DIR__ . '/../config.php';

// Prepare app$app = new Slim\Slim($config['slim']);

Config array goes here

Page 50: Keeping it Small: Getting to know the Slim Micro Framework

Routing

Page 51: Keeping it Small: Getting to know the Slim Micro Framework

Routing

$app->get('/', function () use ($app, $service) { $images = $service->findAll(); $app->render('index.html', array('images' => $images)); });

Page 52: Keeping it Small: Getting to know the Slim Micro Framework

Routing

$app->get('/', function () use ($app, $service) { $images = $service->findAll(); $app->render('index.html', array('images' => $images)); });

HTTP Method

Page 53: Keeping it Small: Getting to know the Slim Micro Framework

Routing

$app->get('/', function () use ($app, $service) { $images = $service->findAll(); $app->render('index.html', array('images' => $images)); });

HTTP Method Resource URI

Page 54: Keeping it Small: Getting to know the Slim Micro Framework

Routing

$app->get('/', function () use ($app, $service) { $images = $service->findAll(); $app->render('index.html', array('images' => $images)); });

HTTP Method Resource URI Anonymous Function

Page 55: Keeping it Small: Getting to know the Slim Micro Framework

Routing

$app->get('/', function () use ($app, $service) { $images = $service->findAll(); $app->render('index.html', array('images' => $images)); });

Page 56: Keeping it Small: Getting to know the Slim Micro Framework

Routing

$app->get('/', function () use ($app, $service) { $images = $service->findAll(); $app->render('index.html', array('images' => $images)); });

Grabs all the pics

Page 57: Keeping it Small: Getting to know the Slim Micro Framework

Routing

$app->get('/', function () use ($app, $service) { $images = $service->findAll(); $app->render('index.html', array('images' => $images)); });

Grabs all the pics

Passes array of image data to index.html

Page 58: Keeping it Small: Getting to know the Slim Micro Framework

GET

$app->get('/:day', function($day) use ($app, $service) { $image = $service->find($day); if (!$image) { $app->notFound(); }

$app->render('images.html', $image); })->conditions(array('day' => '([1-9]\d?|[12]\d\d|3[0-5]\d|36[0-6])'));

Page 59: Keeping it Small: Getting to know the Slim Micro Framework

GET

$app->get('/:day', function($day) use ($app, $service) { $image = $service->find($day); if (!$image) { $app->notFound(); }

$app->render('images.html', $image); })->conditions(array('day' => '([1-9]\d?|[12]\d\d|3[0-5]\d|36[0-6])'));

URL parameter

Page 60: Keeping it Small: Getting to know the Slim Micro Framework

GET

$app->get('/:day', function($day) use ($app, $service) { $image = $service->find($day); if (!$image) { $app->notFound(); }

$app->render('images.html', $image); })->conditions(array('day' => '([1-9]\d?|[12]\d\d|3[0-5]\d|36[0-6])'));

URL parameter... gets passed as an

argument

Page 61: Keeping it Small: Getting to know the Slim Micro Framework

GET

$app->get('/:day', function($day) use ($app, $service) { $image = $service->find($day); if (!$image) { $app->notFound(); }

$app->render('images.html', $image); })->conditions(array('day' => '([1-9]\d?|[12]\d\d|3[0-5]\d|36[0-6])'));

URL parameter... gets passed as an

argument

Condition

Page 62: Keeping it Small: Getting to know the Slim Micro Framework

GET

$app->get('/:day', function($day) use ($app, $service) { $image = $service->find($day); if (!$image) { $app->notFound(); }

$app->render('images.html', $image); })->conditions(array('day' => '([1-9]\d?|[12]\d\d|3[0-5]\d|36[0-6])'));

URL parameter... gets passed as an

argument

Condition 1 to 366

Page 63: Keeping it Small: Getting to know the Slim Micro Framework

GET

$app->get('/:day', function($day) use ($app, $service) { $image = $service->find($day); if (!$image) { $app->notFound(); }

$app->render('images.html', $image); })->conditions(array('day' => '([1-9]\d?|[12]\d\d|3[0-5]\d|36[0-6])'));

404!

Page 64: Keeping it Small: Getting to know the Slim Micro Framework

POST (with redirect)

$app->post('/admin/add-photo', function() use ($app, $service) { $data = $app->request()->post(); $service->save($data); $app->redirect('/admin'); });

Page 65: Keeping it Small: Getting to know the Slim Micro Framework

POST (with redirect)

$app->post('/admin/add-photo', function() use ($app, $service) { $data = $app->request()->post(); $service->save($data); $app->redirect('/admin'); });

$_POST data is in the request object

Page 66: Keeping it Small: Getting to know the Slim Micro Framework

POST (with redirect)

$app->post('/admin/add-photo', function() use ($app, $service) { $data = $app->request()->post(); $service->save($data); $app->redirect('/admin'); });

$_POST data is in the request object

302 Redirect

Page 67: Keeping it Small: Getting to know the Slim Micro Framework

Multiple methods

$app->map('/login', function() { // Login })->via('GET', 'POST');

Page 68: Keeping it Small: Getting to know the Slim Micro Framework

Multiple methods

$app->map('/login', function() { // Login })->via('GET', 'POST');

Not an HTTP Method

Page 69: Keeping it Small: Getting to know the Slim Micro Framework

Multiple methods

$app->map('/login', function() { // Login })->via('GET', 'POST');

Not an HTTP Method

via() is the awesome sauce

Page 70: Keeping it Small: Getting to know the Slim Micro Framework

Logging and flash messaging

Page 71: Keeping it Small: Getting to know the Slim Micro Framework

$app->post('/admin/clear-cache', function() use ($app) {

$log = $app->getLog(); $cleared = null; $clear = $app->request()->post('clear');

if ($clear == 1) { if (apc_clear_cache('user')) { $cleared = 'Cache was successfully cleared!'; } else { $cleared = 'Cache was not cleared!'; $log->error('Cache not cleared'); } }

$app->flash('cleared', $cleared); $app->redirect('/admin'); });

Page 72: Keeping it Small: Getting to know the Slim Micro Framework

$app->post('/admin/clear-cache', function() use ($app) {

$log = $app->getLog(); $cleared = null; $clear = $app->request()->post('clear');

if ($clear == 1) { if (apc_clear_cache('user')) { $cleared = 'Cache was successfully cleared!'; } else { $cleared = 'Cache was not cleared!'; $log->error('Cache not cleared'); } }

$app->flash('cleared', $cleared); $app->redirect('/admin'); });

Get the log from $app

Page 73: Keeping it Small: Getting to know the Slim Micro Framework

$app->post('/admin/clear-cache', function() use ($app) {

$log = $app->getLog(); $cleared = null; $clear = $app->request()->post('clear');

if ($clear == 1) { if (apc_clear_cache('user')) { $cleared = 'Cache was successfully cleared!'; } else { $cleared = 'Cache was not cleared!'; $log->error('Cache not cleared'); } }

$app->flash('cleared', $cleared); $app->redirect('/admin'); });

Get the log from $app

Error!

Page 74: Keeping it Small: Getting to know the Slim Micro Framework

$app->post('/admin/clear-cache', function() use ($app) {

$log = $app->getLog(); $cleared = null; $clear = $app->request()->post('clear');

if ($clear == 1) { if (apc_clear_cache('user')) { $cleared = 'Cache was successfully cleared!'; } else { $cleared = 'Cache was not cleared!'; $log->error('Cache not cleared'); } }

$app->flash('cleared', $cleared); $app->redirect('/admin'); });

Get the log from $app

Error!

Flash message available in the next request.

Page 75: Keeping it Small: Getting to know the Slim Micro Framework

Middleware

“. . . a Slim application can have middleware that may inspect, analyze, or modify the application environment, request, and response before and/or after the Slim application is invoked.”

http://docs.slimframework.com/pages/middleware-overview/

Page 76: Keeping it Small: Getting to know the Slim Micro Framework

Hooks

Page 77: Keeping it Small: Getting to know the Slim Micro Framework

slim.before

Hooks

Page 78: Keeping it Small: Getting to know the Slim Micro Framework

slim.before

slim.before.router

Hooks

Page 79: Keeping it Small: Getting to know the Slim Micro Framework

slim.before

slim.before.router

slim.before.dispatch

Hooks

Page 80: Keeping it Small: Getting to know the Slim Micro Framework

slim.before

slim.before.router

slim.before.dispatch

slim.after.dispatch

Hooks

Page 81: Keeping it Small: Getting to know the Slim Micro Framework

slim.before

slim.before.router

slim.before.dispatch

slim.after.dispatch

slim.after.router

Hooks

Page 82: Keeping it Small: Getting to know the Slim Micro Framework

slim.before

slim.before.router

slim.before.dispatch

slim.after.dispatch

slim.after.router

slim.after

Hooks

Page 83: Keeping it Small: Getting to know the Slim Micro Framework

slim.before

slim.before.router

slim.before.dispatch

slim.after.dispatch

slim.after.router

slim.after

Hooks

Page 84: Keeping it Small: Getting to know the Slim Micro Framework

class MyMiddleware extends \Slim\Middleware{ public function call() { //The Slim application $app = $this->app;

//The Environment object $env = $app->environment();

//The Request object $req = $app->request();

//The Response object $res = $app->response();

//Optionally call the next middleware $this->next->call(); }}

Page 85: Keeping it Small: Getting to know the Slim Micro Framework

class MyMiddleware extends \Slim\Middleware{ public function call() { //The Slim application $app = $this->app;

//The Environment object $env = $app->environment();

//The Request object $req = $app->request();

//The Response object $res = $app->response();

//Optionally call the next middleware $this->next->call(); }}

Extend this

Page 86: Keeping it Small: Getting to know the Slim Micro Framework

class MyMiddleware extends \Slim\Middleware{ public function call() { //The Slim application $app = $this->app;

//The Environment object $env = $app->environment();

//The Request object $req = $app->request();

//The Response object $res = $app->response();

//Optionally call the next middleware $this->next->call(); }}

Extend this

Define call()

Page 87: Keeping it Small: Getting to know the Slim Micro Framework

class MyMiddleware extends \Slim\Middleware{ public function call() { //The Slim application $app = $this->app;

//The Environment object $env = $app->environment();

//The Request object $req = $app->request();

//The Response object $res = $app->response();

//Optionally call the next middleware $this->next->call(); }}

Extend this

Define call()

Inspect, analyze, and modify!

Page 88: Keeping it Small: Getting to know the Slim Micro Framework

class MyMiddleware extends \Slim\Middleware{ public function call() { //The Slim application $app = $this->app;

//The Environment object $env = $app->environment();

//The Request object $req = $app->request();

//The Response object $res = $app->response();

//Optionally call the next middleware $this->next->call(); }}

Extend this

Define call()

On to the next!

Inspect, analyze, and modify!

Page 89: Keeping it Small: Getting to know the Slim Micro Framework

Middleware + Hooks = WIN

Page 90: Keeping it Small: Getting to know the Slim Micro Framework

Navigation example

Page 91: Keeping it Small: Getting to know the Slim Micro Framework

namespace Tsf\Middleware;

use \Zend\Authentication\AuthenticationService;

class Navigation extends \Slim\Middleware{

/** * @var \Zend\Authentication\AuthenticationService */ private $auth;

public function __construct(AuthenticationService $auth) { $this->auth = $auth; }

public function call() { // . . . }

}

Page 92: Keeping it Small: Getting to know the Slim Micro Framework

namespace Tsf\Middleware;

use \Zend\Authentication\AuthenticationService;

class Navigation extends \Slim\Middleware{

/** * @var \Zend\Authentication\AuthenticationService */ private $auth;

public function __construct(AuthenticationService $auth) { $this->auth = $auth; }

public function call() { // . . . }

}

extends

Page 93: Keeping it Small: Getting to know the Slim Micro Framework

namespace Tsf\Middleware;

use \Zend\Authentication\AuthenticationService;

class Navigation extends \Slim\Middleware{

/** * @var \Zend\Authentication\AuthenticationService */ private $auth;

public function __construct(AuthenticationService $auth) { $this->auth = $auth; }

public function call() { // . . . }

}

Constructor injection FTW

extends

Page 94: Keeping it Small: Getting to know the Slim Micro Framework

public function call(){ $app = $this->app; $auth = $this->auth; $req = $app->request();

$home = array('caption' => 'Home', 'href' => '/'); $admin = array('caption' => 'Admin', 'href' => '/admin'); $login = array('caption' => 'Login', 'href' => '/login'); $logout = array('caption' => 'Logout', 'href' => '/logout'); if ($auth->hasIdentity()) { $navigation = array($home, $admin, $logout); } else { $navigation = array($home, $login); }

// . . .}

Page 95: Keeping it Small: Getting to know the Slim Micro Framework

public function call(){ $app = $this->app; $auth = $this->auth; $req = $app->request();

$home = array('caption' => 'Home', 'href' => '/'); $admin = array('caption' => 'Admin', 'href' => '/admin'); $login = array('caption' => 'Login', 'href' => '/login'); $logout = array('caption' => 'Logout', 'href' => '/logout'); if ($auth->hasIdentity()) { $navigation = array($home, $admin, $logout); } else { $navigation = array($home, $login); }

// . . .}

Arrays of nav items

Page 96: Keeping it Small: Getting to know the Slim Micro Framework

public function call(){ $app = $this->app; $auth = $this->auth; $req = $app->request();

$home = array('caption' => 'Home', 'href' => '/'); $admin = array('caption' => 'Admin', 'href' => '/admin'); $login = array('caption' => 'Login', 'href' => '/login'); $logout = array('caption' => 'Logout', 'href' => '/logout'); if ($auth->hasIdentity()) { $navigation = array($home, $admin, $logout); } else { $navigation = array($home, $login); }

// . . .}

Arrays of nav items

Nav differs based on auth status

Page 97: Keeping it Small: Getting to know the Slim Micro Framework

public function call(){ // . . .

$this->app->hook('slim.before.router', function () use (...) {

foreach ($navigation as &$link) { if ($link['href'] == $req->getPath()) { $link['class'] = 'active'; } else { $link['class'] = ''; } }

$app->view() ->appendData(array('navigation' => $navigation)); } );

$this->next->call();}

Page 98: Keeping it Small: Getting to know the Slim Micro Framework

public function call(){ // . . .

$this->app->hook('slim.before.router', function () use (...) {

foreach ($navigation as &$link) { if ($link['href'] == $req->getPath()) { $link['class'] = 'active'; } else { $link['class'] = ''; } }

$app->view() ->appendData(array('navigation' => $navigation)); } );

$this->next->call();}

Delicious hook goodness

Page 99: Keeping it Small: Getting to know the Slim Micro Framework

public function call(){ // . . .

$this->app->hook('slim.before.router', function () use (...) {

foreach ($navigation as &$link) { if ($link['href'] == $req->getPath()) { $link['class'] = 'active'; } else { $link['class'] = ''; } }

$app->view() ->appendData(array('navigation' => $navigation)); } );

$this->next->call();}

Delicious hook goodness

Match dispatched path

Page 100: Keeping it Small: Getting to know the Slim Micro Framework

public function call(){ // . . .

$this->app->hook('slim.before.router', function () use (...) {

foreach ($navigation as &$link) { if ($link['href'] == $req->getPath()) { $link['class'] = 'active'; } else { $link['class'] = ''; } }

$app->view() ->appendData(array('navigation' => $navigation)); } );

$this->next->call();}

Delicious hook goodness

Match dispatched path

Append $navigation to

view

Page 101: Keeping it Small: Getting to know the Slim Micro Framework

public function call(){ // . . .

$this->app->hook('slim.before.router', function () use (...) {

foreach ($navigation as &$link) { if ($link['href'] == $req->getPath()) { $link['class'] = 'active'; } else { $link['class'] = ''; } }

$app->view() ->appendData(array('navigation' => $navigation)); } );

$this->next->call();}

Delicious hook goodness

Match dispatched path

Append $navigation to

viewOn to the next!

Page 102: Keeping it Small: Getting to know the Slim Micro Framework

Views

Page 103: Keeping it Small: Getting to know the Slim Micro Framework

Two great tastes that taste great together

Page 104: Keeping it Small: Getting to know the Slim Micro Framework

Twig

Page 105: Keeping it Small: Getting to know the Slim Micro Framework

Twig

Concise

Page 106: Keeping it Small: Getting to know the Slim Micro Framework

Twig

Concise

Template oriented

Page 107: Keeping it Small: Getting to know the Slim Micro Framework

Twig

Concise

Template oriented

Fast

Page 108: Keeping it Small: Getting to know the Slim Micro Framework

Twig

Concise

Template oriented

Fast

Multiple inheritance

Page 109: Keeping it Small: Getting to know the Slim Micro Framework

Twig

Concise

Template oriented

Fast

Multiple inheritance

Blocks

Page 110: Keeping it Small: Getting to know the Slim Micro Framework

Twig

Concise

Template oriented

Fast

Multiple inheritance

Blocks

Automatic escaping

Page 111: Keeping it Small: Getting to know the Slim Micro Framework

layout.html and

index.html

Page 112: Keeping it Small: Getting to know the Slim Micro Framework

layout.html

Page 113: Keeping it Small: Getting to know the Slim Micro Framework

<title>{% block page_title %} {% endblock %}</title>

Page 114: Keeping it Small: Getting to know the Slim Micro Framework

<ul class="nav"> {% for link in navigation %} <li class="{{link.class}}"> <a href="{{link.href}}">{{link.caption}}</a> </li> {% endfor %}</ul>

Page 115: Keeping it Small: Getting to know the Slim Micro Framework

<h1>365 Days of Photography</h1><h3>Photographer: Jeremy Kendall</h3>{% block content %} {% endblock %}<hr />

Page 116: Keeping it Small: Getting to know the Slim Micro Framework

index.html

Page 117: Keeping it Small: Getting to know the Slim Micro Framework

{% extends 'layout.html' %}

{% block page_title %}365.jeremykendall.net{% endblock %}

{% block content %}{% for image in images %}<div class="row"> <div class="span6"> <h2><a href="/{{image.day}}">{{image.day}}/365</a></h2> <p> <a href="/{{image.day}}"> <img src="{{image.sizes.size.5.source}}" /> </a> </p> <p>Posted {{image.posted|date("m/d/Y")}}</p> </div></div>{% else %}<p>No images in project</p>{% endfor %}{% endblock %}

Page 118: Keeping it Small: Getting to know the Slim Micro Framework

{% extends 'layout.html' %}

{% block page_title %}365.jeremykendall.net{% endblock %}

{% block content %}{% for image in images %}<div class="row"> <div class="span6"> <h2><a href="/{{image.day}}">{{image.day}}/365</a></h2> <p> <a href="/{{image.day}}"> <img src="{{image.sizes.size.5.source}}" /> </a> </p> <p>Posted {{image.posted|date("m/d/Y")}}</p> </div></div>{% else %}<p>No images in project</p>{% endfor %}{% endblock %}

extends

Page 119: Keeping it Small: Getting to know the Slim Micro Framework

{% extends 'layout.html' %}

{% block page_title %}365.jeremykendall.net{% endblock %}

{% block content %}{% for image in images %}<div class="row"> <div class="span6"> <h2><a href="/{{image.day}}">{{image.day}}/365</a></h2> <p> <a href="/{{image.day}}"> <img src="{{image.sizes.size.5.source}}" /> </a> </p> <p>Posted {{image.posted|date("m/d/Y")}}</p> </div></div>{% else %}<p>No images in project</p>{% endfor %}{% endblock %}

extends

<title />

Page 120: Keeping it Small: Getting to know the Slim Micro Framework

{% extends 'layout.html' %}

{% block page_title %}365.jeremykendall.net{% endblock %}

{% block content %}{% for image in images %}<div class="row"> <div class="span6"> <h2><a href="/{{image.day}}">{{image.day}}/365</a></h2> <p> <a href="/{{image.day}}"> <img src="{{image.sizes.size.5.source}}" /> </a> </p> <p>Posted {{image.posted|date("m/d/Y")}}</p> </div></div>{% else %}<p>No images in project</p>{% endfor %}{% endblock %}

extends

<title />

Page 121: Keeping it Small: Getting to know the Slim Micro Framework

{% extends 'layout.html' %}

{% block page_title %}365.jeremykendall.net{% endblock %}

{% block content %}{% for image in images %}<div class="row"> <div class="span6"> <h2><a href="/{{image.day}}">{{image.day}}/365</a></h2> <p> <a href="/{{image.day}}"> <img src="{{image.sizes.size.5.source}}" /> </a> </p> <p>Posted {{image.posted|date("m/d/Y")}}</p> </div></div>{% else %}<p>No images in project</p>{% endfor %}{% endblock %}

extends

<title />iterator

Page 122: Keeping it Small: Getting to know the Slim Micro Framework

{% extends 'layout.html' %}

{% block page_title %}365.jeremykendall.net{% endblock %}

{% block content %}{% for image in images %}<div class="row"> <div class="span6"> <h2><a href="/{{image.day}}">{{image.day}}/365</a></h2> <p> <a href="/{{image.day}}"> <img src="{{image.sizes.size.5.source}}" /> </a> </p> <p>Posted {{image.posted|date("m/d/Y")}}</p> </div></div>{% else %}<p>No images in project</p>{% endfor %}{% endblock %}

extends

<title />iterator

else

Page 123: Keeping it Small: Getting to know the Slim Micro Framework

{% extends 'layout.html' %}

{% block page_title %}365.jeremykendall.net{% endblock %}

{% block content %}{% for image in images %}<div class="row"> <div class="span6"> <h2><a href="/{{image.day}}">{{image.day}}/365</a></h2> <p> <a href="/{{image.day}}"> <img src="{{image.sizes.size.5.source}}" /> </a> </p> <p>Posted {{image.posted|date("m/d/Y")}}</p> </div></div>{% else %}<p>No images in project</p>{% endfor %}{% endblock %}

extends

<title />iterator

elseformat

Page 124: Keeping it Small: Getting to know the Slim Micro Framework

login.html

Page 125: Keeping it Small: Getting to know the Slim Micro Framework

{% extends 'layout.html' %}

{% block page_title %}365.jeremykendall.net | Login{% endblock %}

{% block content %}<div class="row"> <div class="span4"> <h2>Login</h2> {% if flash.error %} <p style="color: red;">{{flash.error}}</p> {% endif %} <form name="login" id="login" class="well" method="post"> // Login form . . . </form> </div></div>

{% endblock %}

Page 126: Keeping it Small: Getting to know the Slim Micro Framework

{% extends 'layout.html' %}

{% block page_title %}365.jeremykendall.net | Login{% endblock %}

{% block content %}<div class="row"> <div class="span4"> <h2>Login</h2> {% if flash.error %} <p style="color: red;">{{flash.error}}</p> {% endif %} <form name="login" id="login" class="well" method="post"> // Login form . . . </form> </div></div>

{% endblock %}

Page 127: Keeping it Small: Getting to know the Slim Micro Framework

The other viewswould be redundant

Page 128: Keeping it Small: Getting to know the Slim Micro Framework

GOTO 0

Page 129: Keeping it Small: Getting to know the Slim Micro Framework

Small but powerfulGOTO 0

Page 130: Keeping it Small: Getting to know the Slim Micro Framework

Small but powerful

Excellent tools to write elegant code

GOTO 0

Page 131: Keeping it Small: Getting to know the Slim Micro Framework

Small but powerful

Excellent tools to write elegant code

Routing, middleware & hooks, views

GOTO 0

Page 132: Keeping it Small: Getting to know the Slim Micro Framework

Small but powerful

Excellent tools to write elegant code

Routing, middleware & hooks, views

I just scratched the surface

GOTO 0

Page 133: Keeping it Small: Getting to know the Slim Micro Framework

Read

Slim: slimframework.com

Twig: twig.sensiolabs.org

Composer: getcomposer.org

MicroPHP Manifesto: microphp.org

Flaming Archer: http://git.io/rH0nrg

Page 134: Keeping it Small: Getting to know the Slim Micro Framework

Questions?

Page 135: Keeping it Small: Getting to know the Slim Micro Framework

Thanks!

[email protected]

@jeremykendall