The Slim Framework support forum has moved to http://discourse.slimframework.com. This Tender forum is no longer maintained or monitored.

GET base or first argument?

dodomeki's Avatar

dodomeki

30 Jul, 2012 09:43 PM

It seemed pretty silly to use two routes for both / and /:argument so I'm trying to figure out a way,
which pretty much defaults back to / if a template is not found. Here's an example..

//if there is no argument, default to home.php
$app->get('(/:template)', function ($template) use ($app) {
    // Verify the template is valid
    if(!file_exists("templates/{$template}.php")) $template='home';
    $app->render('includes/header.php');
    $app->render("{$template}.php");
    $app->render('includes/footer.php');
})->name('templates');

So, here's the relative rundown

// defaults to home.php
http://domain.com 


// defaults to home.php
http://domain.com/this-page-doesnt-exist.php


// defaults to this-page-exists.php
http://domain.com/this-page-exists.php
  1. 1 Posted by dodomeki on 30 Jul, 2012 10:06 PM

    dodomeki's Avatar

    nvm, got it

    //dynamic templating engine
    $app->get('/(:template)', function ($template=null) use ($app) {
        // Verify the template is valid
        if(!file_exists("templates/{$template}.php")) $template='home';
        $app->render('includes/header.php');
        $app->render("{$template}.php");
        $app->render('includes/footer.php');
    })->name('templates');
    
  2. Support Staff 2 Posted by Brian Nesbitt on 31 Jul, 2012 02:51 AM

    Brian Nesbitt's Avatar

    You might want to do if ($template == null || !file_exists("templates/{$template}.php")) as it would skip the disk check on the home page which I assume would be your most accessed page.

    Instead of using a header/footer you could look at a real simple master template system I wrote awhile back for Slim, https://github.com/briannesbitt/Slim-Multilingual/blob/master/app/l... and a post explaining it http://nesbot.com/2012/6/26/multilingual-site-using-slim

  3. Brian Nesbitt closed this discussion on 01 Aug, 2012 01:44 PM.

Comments are currently closed for this discussion. You can start a new one.

Keyboard shortcuts

Generic

? Show this help
ESC Blurs the current field

Comment Form

r Focus the comment reply box
^ + ↩ Submit the comment

You can use Command ⌘ instead of Control ^ on Mac