Question

So after fixing mustache and Apache perms I now have ran into an issue where the site will load the index page, but any page after that located in (the same place a index.html) /pages as it fails to load.

What you can see below is the index.php for Slim to do it's stuff. I can't really understand why the index page will load just fine but no other page will load. If you can point me in the right direction then it would be greatly appreciated.

Examples:

End User > myexample.com/

Location > myexample.com/pages/index.html

The above works just fine but if you look below, you should be able to understand my issue.

End User > myexample.com/info

Location > myexample.com/pages/info.html

The End User is what you see on the site, but the location is where the files belong. Thanks in advance, Luke.

<?php

// Load up composer autoload, and instantiate the application.
require 'vendor/autoload.php';

$app = new \Slim\Slim;

// Register a singleton of the Mustache engine, and tell it to cache
$app->container->singleton('mustache', function () {
    return new Mustache_Engine(array(
        'cache' => 'storage',
        'loader' => new Mustache_Loader_FilesystemLoader(dirname(__FILE__) . '/pages', array('extension' => '.html'))
    ));
});

// Helper function to render templates
function renderTemplate($name, $data = array()) {
    global $app;

    if (file_exists('config.php')) {
        $data = (require 'config.php');
    }

    $data += array(
        'resourceUri' => $app->request->getResourceUri() ?: 'index',
        'request' => $app->request
    );

    return $app->mustache->loadTemplate($name)->render($data);
}

// Loads a page by the given name/path
function loadPage($path) {
    global $app;

    // Set up the base path
    $f = 'pages/' . $path;

    if (file_exists($f . '.html')) {
        // If there's an HTML file, render the mustache template
        return renderTemplate($path . '.html');
    } elseif (file_exists($f . '.php')) {
        // If there's a PHP file, return it
        return (require $f . '.php');
    } elseif ($path != '404') {
        // Otherwise, go get the 404 page
        return loadPage('404');
    } else {
        // But if the user doesn't have a 404 page made, return a plain 404
        $app->halt(404);
    }
}

// Index page
$app->get('/', function () use ($app) {
    echo loadPage('index');
});


// Route to everything else
$app->get('/.*?', function () use ($app) {
    // Get the current request path
    $path = $app->request->getResourceUri();

    // Make sure the user isn't trying to escape and do nasty things
    if (!preg_match('/^[A-z\/\-\_]+$/', $path)) {
        echo loadPage('404');
    }

    // Send the page off to get loaded
    echo loadPage($path);
});
$app->run();
Was it helpful?

Solution

I think you'll find your problem isn't with Slim but rather with the custom functions you wrote to allow Slim to load Mustache templates.

I would highly recommend removing those functions and using the custom Slim Mustache view provided in the views directory of Slim-Extras repo. At that point, any issues with Slim can be more easily diagnosed as there won't be custom functions to debug.

NOTE: While the Slim-Extras repo is deprecated in favor of the Slim-Views repo, the custom Mustache view (which hasn't made it to the Slim-Views repo) should work fine.

For reference please see the Custom Views section of the Slim documentation.

UPDATE: I've been using Dearon's Slim Mustache library for integration with Slim View in a new app of mine and Justin Hileman's PHP implementation of Mustache. Both are highly recommended and simple to install and configure. Good luck!

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top