I have some simple app in PHP Slim (newest version) , it's a simple website that gets data from database and display it on twig templates. Nothing fancy. Everything works perfect as long as i don't try to add custom function to twig.
When i add custom function to twig, on some servers(on localhost laragoon works fine,on production server with the same php version not i get this error :
Fatal error: Cannot instantiate abstract class Twig_Function in ... twig.php on line 4
below is my twig.php - error is connected to new Twig_Function ,but honestly i don't see any abstract class here ..., and on localhost Laragon it's working fine , on production
Anyone have any idea how to solve this problem ?
<?php
$twig=$container->get('view')->getEnvironment();
$function = new Twig_Function('fotki', function ($id) use ($container) {
$files=array();
$dir=opendir($container->get('settings')['fotki_path'].(int)$id);
while($file=readdir($dir)){
if((strlen($file)>3)&&(substr($file,-3)=='jpg')){
array_push($files,(int)$id.'/'.$file);
//
}
}
closedir($dir);
return $files;
});
$twig->addFunction($function);
$function = new Twig_Function('fotkalink', function ($path) use ($container) {
$tab=explode('/',$path);
return 'mediaimage/'.$tab[0].'-'.$tab[1];
});
$twig->addFunction($function);
Declaration of view in container :
$container['view'] = function ($c) {
$a=[];
if($c['settings']['env']=='prod')
$a=[ 'cache' => __DIR__.'/../cache'];
$view = new \Slim\Views\Twig( __DIR__.'/../templates', $a);
$basePath = rtrim(str_ireplace('index.php', '', $c['request']->getUri()->getBasePath()), '/');
$view->addExtension(new Slim\Views\TwigExtension($c['router'], $basePath));
return $view;
};
You need to make use of the Twig_SimpleFunction which extends of Twig_Function as you can't instantiate an abstract class.
$function = new Twig_SimpleFunction('fotkalink', function ($path) use ($container) {
$tab=explode('/',$path);
return 'mediaimage/'.$tab[0].'-'.$tab[1];
});
$twig->addFunction($function);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With