25. REQUIRED PARAM
?php
require 'Slim/Slim.php';
$app = new Slim();
$app-get('/', function() {
echo 'Hello World from base route.';
});
//param name is required
$app-get('/hello_to/:name', function($name) {
echo 'Hello World to ' . $name;
});
$app-run();
25
26. OPTIONAL PARAM
?php
require 'Slim/Slim.php';
$app = new Slim();
$app-get('/', function() {
echo 'Hello World from base route.';
});
//when using optional params, you have to define default value for function
param
$app-get('/hello_to(/:name)', function($name = null) {
if ($name === null) {
$name = 'John Doe';
}
echo 'Hello World to ' . $name;
});
$app-run();
26
27. NAMED ROUTES
?php
require 'Slim/Slim.php';
$app = new Slim();
$app-get('/', function() use ($app) {
echo 'Hello World from base route.br';
$url = $app-urlFor('hello', array('name' = 'Jimmy')); //create link for route
$link = sprintf('a href=%s%s/a', $url, $url);
echo 'Oh, link to hello page for Jimmy is ' . $link;
});
$app-get('/hello_to(/:name)', function($name = null) {
if ($name === null) {
$name = 'John Doe';
}
echo 'Hello World to ' . $name;
})-name('hello'); //using name for route
$app-run();
27
28. ROUTE CONDITIONS
?php
require 'Slim/Slim.php';
$app = new Slim();
$app-get('/', function() use ($app) {
echo 'Hello World from base route.br';
$url = $app-urlFor('hello', array('name' = 'Jimmy'));
$link = sprintf('a href=%s%s/a', $url, $url);
echo 'Oh, link to hello page for Jimmy is ' . $link;
});
$app-get('/hello_to(/:name)', function($name = null) {
if ($name === null) {
$name = 'John Doe';
}
echo 'Hello World to ' . $name;
})-name('hello')
-conditions(array('name' = '[A-Za-z]+')); //use only letters as param 'name'
$app-run();
28
29. REDIRECT
?php
require 'Slim/Slim.php';
$app = new Slim();
/* ... */
$app-get('/hello_to(/:name)', function($name = null) {
if ($name === null) {
$name = 'John Doe';
}
echo 'Hello World to ' . $name;
})-name('hello')
-conditions(array('name' = '[A-Za-z]+'));
//redirect to default hello page
$app-get('/redirect', function() use ($app) {
$app-redirect($app-urlFor('hello'));
});
$app-run();
29
30. REDIRECT WITH STATUS
?php
require 'Slim/Slim.php';
$app = new Slim();
/* ... */
$app-get('/hello_to(/:name)', function($name = null) {
if ($name === null) {
$name = 'John Doe';
}
echo 'Hello World to ' . $name;
})-name('hello')
-conditions(array('name' = '[A-Za-z]+'));
//redirect to default hello page as 301, not 302 which is default
$app-get('/redirect', function() use ($app) {
$app-redirect($app-urlFor('hello'), 301);
});
$app-run();
30
31. MIDDLEWARE
?php
require 'Slim/Slim.php';
$app = new Slim();
$app-get('/',
function() {
//this will be executed before main callable
echo Hello, I'm middleware br;
},
function() use ($app) {
echo 'Hello World from base route.br';
$url = $app-urlFor('hello', array('name' = 'Jimmy'));
$link = sprintf('a href=%s%s/a', $url, $url);
echo 'Oh, link to hello page for Jimmy is ' . $link;
});
/* ... */
$app-run();
31
32. MIDDLEWARE
?php
require 'Slim/Slim.php';
$app = new Slim();
$app-get('/',
function() {
//this will be executed before main callable
echo Hello, I'm middleware br;
},
function() {
//this will be executed before main callable
echo And I'm second middleware br;
},
function() use ($app) {
echo 'Hello World from base route.br';
$url = $app-urlFor('hello', array('name' = 'Jimmy'));
$link = sprintf('a href=%s%s/a', $url, $url);
echo 'Oh, link to hello page for Jimmy is ' . $link;
});
/* ... */
$app-run();
32
33. MIDDLEWARE
?php
require 'Slim/Slim.php';
$app = new Slim();
$app-get('/',
function() {
//this will be executed before main callable
echo Hello, I'm middleware br;
},
function() {
//this will be executed before main callable
echo And I'm second middleware br;
},
function() use ($app) {
echo 'Hello World from base route.br';
Andsoon-everythingbeforelastcallableis
$url = $app-urlFor('hello', array('name' = 'Jimmy'));
$link = sprintf('a href=%s%s/a', $url, $url);
middleware
echo 'Oh, link to hello page for Jimmy is ' . $link;
});
/* ... */
$app-run();
33
34. VIEW
?php
//file index.php
require 'Slim/Slim.php';
$app = new Slim();
$app-get('/', function() use ($app) {
$url = $app-urlFor('hello', array('name' = 'Jimmy'));
//default path is __DIR__ . /templates
return $app-render('view.php', compact('url'));
});
/* ... */
$app-run();
Hello World from base route. br
Oh, link to hello page for Jimmy is a href=?php echo $url??php echo
$url?/a
34
35. HTTP CACHE - ETAG
?php
require 'Slim/Slim.php';
$app = new Slim();
/* ... */
$app-get('/hello_to(/:name)', function($name = null) use ($app) {
if ($name === null) {
$name = 'John Doe';
}
//auto ETag based on some id - next request with the same name will return 304
Not Modified
$app-etag($name);
echo 'Hello World to ' . $name;
})-name('hello')
-conditions(array('name' = '[A-Za-z]+'));
/* ... */
$app-run();
35
36. HTTP CACHE - TIME BASED
?php
require 'Slim/Slim.php';
$app = new Slim();
/* ... */
$app-get('/hello_to(/:name)', function($name = null) use ($app) {
if ($name === null) {
$name = 'John Doe';
}
$app-lastModified(1327305485); //cache based on time
echo 'Hello World to ' . $name;
})-name('hello')
-conditions(array('name' = '[A-Za-z]+'));
/* ... */
$app-run();
36
37. FLASH MESSAGE
?php
require 'Slim/Slim.php';
$app = new Slim();
$app-get('/', function() use ($app) {
$url = $app-urlFor('hello', array('name' = 'Jimmy'));
return $app-render('view.php', compact('url'));
});
//redirect to default page with flash message which will be displayed once
$app-get('/redirect', function() use ($app) {
$app-flash('info', You were redirected);
$app-redirect($app-request()-getRootUri());
});
$app-run();
?php echo $flash['info'] ?
Hello World from base route. br
Oh, link to hello page for Jimmy is a href=?php echo $url??php echo $url?/a
37
38. CUSTOM 404
?php
require 'Slim/Slim.php';
$app = new Slim();
//define custom 404 page
$app-notFound(function() {
echo I'm custom 404;
});
$app-get('/hello_to(/:name)', function($name = null) use ($app) {
if ($name === null) {
$name = 'John Doe';
}
$possibleNames = array('Leonard', 'Sheldon', 'John Doe');
//when name not found, force 404 page
if (array_search($name, $possibleNames) === false) {
$app-notFound();
}
echo 'Hello World to ' . $name;
})-name('hello')
-conditions(array('name' = '[A-Za-z]+'));
$app-run();
38
39. CUSTOM 404
?php
require 'Slim/Slim.php';
$app = new Slim();
//define custom 404 page
$app-notFound(function() {
echo I'm custom 404;
});
$app-get('/hello_to(/:name)', function($name = null) use ($app) {
if ($name === null) {
} Customerrorpage(500)alsopossible
$name = 'John Doe';
$possibleNames = array('Leonard', 'Sheldon', 'John Doe');
//when name not found, force 404 page
if (array_search($name, $possibleNames) === false) {
$app-notFound();
}
echo 'Hello World to ' . $name;
})-name('hello')
-conditions(array('name' = '[A-Za-z]+'));
$app-run();
39
45. PHP 5.2
?php
require 'Slim/Slim.php';
$app = new Slim();
function index() {
global $app;
echo 'Hello World from base route.br';
$url = $app-urlFor('hello', array('Jimmy'));
$link = sprintf('a href=%s%s/a', $url, $url);
echo 'Oh, link to hello page for Jimmy is ' . $link;
}
//last param must return true for is_callable call, so that it's valid
$app-get('/', 'index');
/* ... */
$app-run();
45
46. PHP 5.2
?php
require 'Slim/Slim.php';
$app = new Slim();
function index() {
global $app;
echo 'Hello World from base route.br';
$url = $app-urlFor('hello', array('Jimmy'));
$link = sprintf('a href=%s%s/a', $url, $url);
echo 'Oh, link to hello page for Jimmy is ' . $link;
}
//last param must return true for is_callable call, so that it's valid
Somebodysaid,that:everytime,whenyouuse
$app-get('/', 'index');
/* ... */
global,unicorndies;)
$app-run();
46
49. PHP 5.2
?php
class Controller {
public static $app;
public static function index() {
echo 'Hello World from base route.br';
$url = self::$app-urlFor('hello', array('Jimmy'));
$link = sprintf('a href=%s%s/a', $url, $url);
echo 'Oh, link to hello page for Jimmy is ' . $link;
}
}
require 'Slim/Slim.php';
$app = new Slim();
Controller::$app = $app;
//last param must return true for is_callable call, so that it's also valid
$app-get('/', array('Controller', 'index'));
/* ... */
$app-run();
49
51. PHP 5.2
?php
class Controller {
protected $_app;
public function __construct(Slim $app) {
$this-_app = $app;
}
public function index() {
echo 'Hello World from base route.br';
$url = $this-_app-urlFor('hello', array('Jimmy'));
$link = sprintf('a href=%s%s/a', $url, $url);
echo 'Oh, link to hello page for Jimmy is ' . $link;
}
}
require 'Slim/Slim.php';
$app = new Slim();
$controller = new Controller($app);
//last param must return true for is_callable call, so that it's also valid
$app-get('/', array($controller, 'index'));
/* ... */
$app-run();
51
52. SO, DO YOU REALLY NEED
THAT ?
Source: http://www.rungmasti.com/2011/05/swiss-army-knife/
52