Last active
July 22, 2019 10:21
-
-
Save Atulin/deb28f36cb7ef83e22249b1cea106a88 to your computer and use it in GitHub Desktop.
Making Altorouter work with Twig
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/* Project structure | |
* | |
* ROOT | |
* - index.php | |
* - public | |
* - master.twig | |
*/ | |
require 'vendor/autoload.php'; | |
$router = new AltoRouter(); | |
// Map routes | |
try { | |
$router->map('GET', '/', function (){return __DIR__.'/master.twig';}, '/master.twig'); | |
} catch (Exception $e) { | |
header('Content-type: application/json'); | |
echo json_encode('Error [0]: '.$e); | |
} | |
$loader = new Twig_Loader_Filesystem(array(__DIR__.'/public')); | |
$twig = new Twig_Environment($loader); | |
$match = $router->match(); | |
if( $match && is_callable( $match['target'] ) ) { | |
try { | |
// Render the actual Twig template | |
echo $twig->render($match['name'], array()); | |
// Handle all possible errors | |
} catch (Twig_Error_Loader $e) { | |
header('Content-type: application/json'); | |
echo json_encode('Error [1]: '.$e); | |
} catch (Twig_Error_Runtime $e) { | |
header('Content-type: application/json'); | |
echo json_encode('Error [2]: '.$e); | |
} catch (Twig_Error_Syntax $e) { | |
header('Content-type: application/json'); | |
echo json_encode('Error [3]: '.$e); | |
} | |
} else { | |
header( $_SERVER['SERVER_PROTOCOL'] . ' 404 Not Found'); | |
echo var_export($match); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment