Example #1
0
include 'models/snippets.class.php';
$snippets_model = new Snippets_model($pdo);
$app = new Silex\Application();
$app['debug'] = true;
// Twig
$app->register(new Silex\Provider\TwigServiceProvider(), array('twig.path' => __DIR__ . '/views'));
$app->register(new Silex\Provider\UrlGeneratorServiceProvider());
// Used for requests
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
$app->before(function () use($app, $snippets_model) {
    $categories = $snippets_model->get_categories();
    $app["twig"]->addGlobal("categories", $categories);
});
$app->get('/', function ($page) use($app, $snippets_model) {
    $snippets = $snippets_model->get_all();
    $pages = $snippets_model->get_pages_all();
    $app["twig"]->addGlobal("actualPage", array("page" => "home", "category" => "all"));
    return $app['twig']->render('snippets.twig', array('snippets' => $snippets, "pages" => $pages));
})->value('page', 1)->bind('home');
$app->get('/category/{category}/{page}', function ($category = "all", $page = 1) use($app, $snippets_model) {
    if ($category == "all") {
        $snippets = $snippets_model->get_all($page);
        $pages = $snippets_model->get_pages_all($page);
        $cat_details = array('slug' => 'all', 'title' => 'All', 'count' => $snippets_model->get_snippets_count());
    } else {
        $cat_details = $snippets_model->get_cat_details($category);
        $cat_details = $cat_details[0];
        $snippets = $snippets_model->get_by_category($category, $page);
        $pages = $snippets_model->get_pages_by_category($cat_details['id'], $page);
    }