Ian Littman (@iansltx)
http://ian.im/slim3july15
<?php require __DIR__ . '/vendor/autoload.php';
$app = new \Slim\App();
$app->get('/', function($req, $res) {
return $res->write('Hello world!');
});
$app->get('/{name}', function($req, $res, $args) {
return $res->write('Hello ' . $args['name'] . '!');
});
$app->run();
composer init -s dev composer require slim/slim php -S localhost:8080 index.php
<?php // $app is instantiated above
$app->add(function($req, $res, $next) { // runs second, right before app
if (!$this->auth->isLoggedIn($req))
return new UnauthedResponse; // assuming it's PSR-7 compliant
return $next($req, $res);
});
$app->add(function($req, $res, $next) { // runs first (outermost)
$startTime = microtime(true);
$res = $next($req->withHeader('Start-Time', $startTime), $res);
return $res->write(
'<!-- Total Time: ' . microtime(true) - $startTime . ' -->');
});
Looks up MyController in the DI container, then calls someAction with the same arguments as a closure would get
Route groups (prefixes) are supported
$container = new \Slim\Container(); // Pimple + Slim defaults
$container['myService'] = function($c) {
return new MyService($c->get('someOtherDependency');
};
// if you provide the expected services,
// any Container Interop DI container will work here
$app = new \Slim\App($container);
$app->get('/', function($req, $res) { // $this is bound to app
// app proxies __get() to container
return $res->write($this->myService->doACoolThing($req);
});
When?
Now: maybe
When released: better idea
How?
APIs: good call
Microservices: yep
If expecting an opinionated framework: maybe not
If you're opinionated: you'll probably like it