public
anonymous / index.php
Created

Expose Slim urlFor to Twig

  • Download Gist
index.php
PHP
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
<?php
// Wrapper function to access urlFor.
function slim_url_for($name, $params = array()) {
return Slim::getInstance()->urlFor($name, $params);
}
 
// Setup Twig.
TwigView::$twigDirectory = '/path/to/twig/';
 
// Expose urlFor to Twig.
$view = new TwigView;
$twig = $view->getEnvironment();
$twig->addFunction('url', new Twig_Function_Function('slim_url_for'));
 
// Setup Slim.
$app = new Slim(array('view' => new TwigView));
 
// Create a named route.
$app->get('/article/(:id)', function($id) {
// Your logic here.
})->name('article_view');
 
/*
* In your templates you can now the following code to generate the URL:
* {{ url('article_view', { 'id': article.id }) }}
*/

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.