Esempio n. 1
0
// Prepare app
$app = new \Slim\Slim(array('templates.path' => '../templates'));
// Create monolog logger and store logger in container as singleton
// (Singleton resources retrieve the same log resource definition each time)
$app->container->singleton('log', function () {
    $log = new \Monolog\Logger('slim-skeleton');
    $log->pushHandler(new \Monolog\Handler\StreamHandler('../logs/app.log', \Psr\Log\LogLevel::DEBUG));
    return $log;
});
// Prepare view
$app->view(new \Slim\Views\Twig());
$app->view->parserOptions = array('charset' => 'utf-8', 'cache' => realpath('../templates/cache'), 'auto_reload' => true, 'strict_variables' => false, 'autoescape' => true);
$app->view->parserExtensions = array(new \Slim\Views\TwigExtension());
// Define routes
$app->get('/', function () use($app) {
    $accept = $app->request->headers->get('Accept');
    if ($accept == 'application/json') {
        $app->redirect('/lbt?version=1', 301);
    }
    $app->render('index.html');
});
// $version and $format aren't currently in use. They're here for future api versioning
$app->get('/lbt(/:version(/:format))', function ($version = 1, $format = 'json') use($app) {
    $jsend = new JSendResponse('success', array('name' => 'Exploits of a Mom', 'permalink' => 'http://xkcd.com/327/', 'image' => 'http://imgs.xkcd.com/comics/exploits_of_a_mom.png', 'alt' => 'Her daughter is named Help I\'m trapped in a driver\'s license factory.', 'link' => $app->request->getPathInfo() . sprintf('?version=%d&format=%s', $version, $format)));
    $app->response->headers->set('Content-Type', 'application/json');
    $app->response->headers->set('X-LBTaaS-Media-Type', 'lbtaas.v1');
    $app->response->setBody($jsend->encode());
    return $app->response;
});
// Run app
$app->run();
Esempio n. 2
0
 public function testAddingEncodeOptions()
 {
     $success = JSendResponse::success(array('some' => 'data'));
     $success->setEncodingOptions(\JSON_PRETTY_PRINT);
     $result = $success->encode();
     $pretty = json_encode($success->asArray(), \JSON_PRETTY_PRINT);
     $this->assertEquals($pretty, $result);
 }
Esempio n. 3
0
 /**
  * @expectedException JSend\InvalidJSendException
  * @expectedExceptionMessage JSend errors must contain a message.
  */
 public function testDecodeErrorMustHaveMessage()
 {
     JSendResponse::decode('{ "status": "error" }');
 }