【发布时间】:2017-07-09 09:58:27
【问题描述】:
我在 PHP Slim(最新版本)中有一些简单的应用程序,它是一个简单的网站,可以从数据库中获取数据并将其显示在树枝模板上。没有什么花哨。 只要我不尝试向树枝添加自定义功能,一切都会完美。
当我将自定义函数添加到树枝时,在某些服务器上(在 localhost laragoon 上工作正常,在具有相同 php 版本的生产服务器上没有我得到这个错误:
致命错误:无法在第 4 行的 ... twig.php 中实例化抽象类 Twig_Function
下面是我的 twig.php - 错误连接到 new Twig_Function ,但老实说我在这里看不到任何抽象类...,在本地主机 Laragon 上它工作正常,在生产中
有人知道如何解决这个问题吗?
<?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);
容器中的视图声明:
$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;
};
【问题讨论】: