Example #1
0
 /**
  * Test default instance properties
  */
 public function testDefaultInstanceProperties()
 {
     $s = new \Slim\Slim();
     $this->assertInstanceOf('\\Slim\\Http\\Request', $s->request());
     $this->assertInstanceOf('\\Slim\\Http\\Response', $s->response());
     $this->assertInstanceOf('\\Slim\\Router', $s->router());
     $this->assertInstanceOf('\\Slim\\View', $s->view());
     $this->assertInstanceOf('\\Slim\\Log', $s->getLog());
     $this->assertEquals(\Slim\Log::DEBUG, $s->getLog()->getLevel());
     $this->assertTrue($s->getLog()->getEnabled());
     $this->assertInstanceOf('\\Slim\\Environment', $s->environment());
 }
Example #2
0
 /**
  * Test get current route
  */
 public function testGetCurrentRoute()
 {
     \Slim\Environment::mock(array('REQUEST_METHOD' => 'GET', 'SCRIPT_NAME' => '', 'PATH_INFO' => '/foo'));
     $app = new \Slim\Slim();
     $route1 = $app->get('/bar', function () {
         echo "Bar";
     });
     $route2 = $app->get('/foo', function () {
         echo "Foo";
     });
     $app->call();
     $this->assertSame($route2, $app->router()->getCurrentRoute());
 }
Example #3
0
    echo 'This is a POST route';
});
// PUT route
$app->put('/put', function () {
    echo 'This is a PUT route';
});
// PATCH route
$app->patch('/patch', function () {
    echo 'This is a PATCH route';
});
// DELETE route
$app->delete('/delete', function () {
    echo 'This is a DELETE route';
});
$app->get('/test', function () use($app) {
    $routes = $app->router()->getNamedRoutes();
    //echo "<br> Routes". print_r($routes);
    $result = array();
    foreach ($routes as $route) {
        $result[$route->getPattern()] = $route->getName();
    }
    print json_encode($result);
    //$cra = new CesRestAPI();
    //print $cra->incrementUserCredit("BTSP0000",11);
    //print $cra->getUserCreditData("BTSP0000");
})->setName('test');
$app->get('/test/db', function () {
    $cra = new CesRestAPI();
    print $cra->initDBConnection(true);
})->setName('test db connection');
/**
Example #4
0
// 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) {
    // Sample log message
    $app->log->info("Slim-Skeleton '/' route");
    // Render index view
    $app->render('index.html');
});
$app->get('/hello/:name', function ($name) {
    echo "Hello, " . $name;
});
$app->get('/who/:name', function ($name) use($app) {
    $router = $app->router();
    print_r($router->getCurrentRoute()->getParams());
});
$app->get('/get/:id', function ($id) use($app) {
    //DB connection with medoo
    // TODO move to init file with config
    $database = new medoo(['database_type' => 'mysql', 'database_name' => 'api', 'server' => '127.0.0.1', 'username' => 'root', 'password' => '', 'charset' => 'utf8', 'port' => 3306, 'option' => [PDO::ATTR_CASE => PDO::CASE_NATURAL]]);
    $datas = $database->select("users", ["username", "email"], ["id" => $id]);
    $app->response->headers->set('Content-Type', 'application/json');
    $app->response->headers->set('Access-Control-Allow-Origin', "http://localhost");
    $app->response->headers->set('Access-Control-Allow-Methods', 'GET,PUT,POST,DELETE');
    $app->response->headers->set('Access-Control-Allow-Headers', 'Content-Type');
    $app->response->body(json_encode($datas));
});
$app->get('/friends/:userId', function ($userId) use($app) {
    $app->response->headers->set('Content-Type', 'application/json');
<?php

session_cache_limiter(false);
session_start();
require 'vendor/autoload.php';
// "USE" STATEMENTS
// use App\Example;
// APP CONFIG
$app = new \Slim\Slim(array('debug' => true, 'mode' => 'dev', 'log.enabled' => true, 'log.path' => 'logs', 'log.level' => 8, 'log.writer' => new \Slim\Logger\DateTimeFileWriter(), 'templates.path' => 'templates', 'cookies.encrypt' => true, 'cookies.secure' => false, 'cookies.httponly' => true, 'cookies.secret_key' => 'CHANGEME', 'cookies.cipher' => MCRYPT_RIJNDAEL_256, 'cookies.cipher_mode' => MCRYPT_MODE_CBC, 'cookies.lifetime' => '2 days', 'view' => new \Slim\Views\Twig()));
// GLOBAL MIDDLEWARES
// $app->add(new \App\CustomErrorMiddleware());
// HOOKS
// $app->hook('slim.before', function () use ($app) { /* ... */ });
// Adds the page variable into template to use as a CSS class for page-specific style.
$app->hook('slim.before.dispatch', function () use($app) {
    $pattern = $app->router()->getCurrentRoute()->getPattern();
    $page = preg_replace('/\\//', '-', $pattern);
    $page = preg_replace('/(\\:([A-Za-z0-9])*)/', '', $page);
    $app->view->setData('page', $page);
});
// ERROR HANDLING
$app->notFound(function () use($app) {
    $app->render('http/404.html');
});
$app->error(function (\Exception $e) use($app) {
    $app->render('http/500.html', array('message' => $e->getMessage()));
});
// TWIG CONFIG
$view = $app->view();
$view->parserDirectory = 'vendor/twig/twig';
$view->parserOptions = array('debug' => true, 'cache' => dirname(__FILE__) . '/cache');
Example #6
0
require 'app/vendor/autoload.php';
// Start Slim
$app = new \Slim\Slim(array('templates.path' => 'web/twig'));
$app->view(new \Slim\Views\Twig());
$app->view->parserExtensions = array(new \Slim\Views\TwigExtension());
$app->view->parserOptions = array('charset' => 'utf-8', 'cache' => realpath('app') . '/cache/twig', 'auto_reload' => true, 'strict_variables' => false, 'autoescape' => true);
// Twig cachebuster
$bustCacheFilter = new Twig_SimpleFilter('bust_cache', function ($string) {
    if (!preg_match('/^(.*)(\\.[^.]*)$/', $string, $matches)) {
        return $string;
    }
    $path = 'web' . $string;
    if (!file_exists($path)) {
        return $string;
    }
    return $matches[1] . '.' . filemtime($path) . $matches[2];
});
$twig = $app->view->getInstance()->addFilter($bustCacheFilter);
// currentRouteName Twig variable
$app->hook('slim.before.dispatch', function () use($app) {
    $app->view()->setData(array('currentRouteName' => $app->router()->getCurrentRoute()->getName()));
});
// Password protect
$app->add(new \Slim\Middleware\HttpBasicAuthentication(["path" => ["/admin", "/api"], "secure" => false, "users" => $users]));
// Include routes
foreach (glob("app/routes/*.php") as $filename) {
    include $filename;
}
// Run app
$app->run();