Skip to content

Instantly share code, notes, and snippets.

@adnasa
Created March 21, 2014 13:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save adnasa/9686702 to your computer and use it in GitHub Desktop.
Save adnasa/9686702 to your computer and use it in GitHub Desktop.
Base index for silex.
<?php
require_once __DIR__.'/../vendor/autoload.php';
use Symfony\Component\HttpFoundation\Request;
$app = new Silex\Application();
$app->register(new Silex\Provider\TranslationServiceProvider(), array(
'translator.messages' => array(),
));
$app->register(new Silex\Provider\TwigServiceProvider(), array(
'twig.path' => __DIR__.'/theme/templates',
));
$app->register(new Silex\Provider\DoctrineServiceProvider(), array(
/*'db.options' => array (
'driver' => 'pdo_mysql',
'host' => 'localhost',
'dbname' => '',
'user' => '',
'password' => '',
'charset' => 'utf8',
)*/
));
/**
* Mapper.
*/
$app['app.mapper'] = $app->share(function() use ($app) {
return new \Application\Model\Mapper\DbMapper($app['db']);
});
/**
* LogController
*/
$app['app.log'] = $app->share(function() use($app) {
return new \Application\Controller\LogController($app['app.mapper']);
});
/**
* ProjectController
*/
$app['app.project'] = $app->share(function() use($app) {
return new \Application\Controller\ProjectController($app['app.mapper']);
});
/**
* endpoints.
*/
$app->get('/', function() use($app) {
$response = array(
'implements' => array(
'/api/v1/logs' => array('GET', 'POST'),
'/api/v1/logs[/:id]' => array(
'PUT',
'DELETE',
),
'/api/v1/projects' => array('GET', 'POST'),
'/api/v1/project[/:id]' => array(
'PUT',
'DELETE',
),
),
// 'session_key' => '',
);
return $app->json($response, 200);
});
$app->get('/api/v1/logs', function() use($app) {
$getList = $app['app.log']->getList();
return $app->json($getList, 200);
});
$app->match('/api/v1/log/{id}', function (Request $request, $id) use ($app) {
$log = $app['app.log']->getLog($id);
if ($log === null) {
$response = array(
'content' => "Log with [id:{$id}], does not exist.",
);
return $app->json($response, 404);
}
return $app->json($log, 200);
});
$app->get('/api/v1/projects', function() use($app) {
});
$app->match('/api/v1/project/{id}', function(Request $request, $id) use($app) {
});
$app->run();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment