Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/283.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 如何配置Twig以查找模板?_Php_Twig_Slim - Fatal编程技术网

Php 如何配置Twig以查找模板?

Php 如何配置Twig以查找模板?,php,twig,slim,Php,Twig,Slim,我将Twig与Slim一起使用,出现以下错误: 警告:文件获取内容(application/templates/config.html):失败 要打开流:中没有这样的文件或目录 /上的var/www/testing/vendor/twig/twig/lib/twig/Loader/Filesystem.php 第131行 下面的脚本位于/var/www/testing/html/index.php,我已经验证了模板存在于/var/www/testing/application/templates

我将Twig与Slim一起使用,出现以下错误:

警告:文件获取内容(application/templates/config.html):失败 要打开流:中没有这样的文件或目录 /上的var/www/testing/vendor/twig/twig/lib/twig/Loader/Filesystem.php 第131行

下面的脚本位于
/var/www/testing/html/index.php
,我已经验证了模板存在于
/var/www/testing/application/templates/config.html

$container['view'] = function ($c) {
    $view = new \Slim\Views\Twig('../application/templates', [
        //'cache' => 'path/to/cache'    // See auto_reload option
        'debug' => true,
        'strict_variables'=> true
    ]);
    $view->addExtension(new \Slim\Views\TwigExtension(
        $c['router'],
        $c['request']->getUri()
    ));
    $view->addExtension(new \Twig_Extension_Debug());
    return $view;
};

$app->get('/config', function (Request $request, Response $response) {
    return $this->view->render($response, 'config.html',[]);
});
public function getSource($name)
{
    return file_get_contents($this->findTemplate($name));
}
第131行如下所示,返回
config.html

$container['view'] = function ($c) {
    $view = new \Slim\Views\Twig('../application/templates', [
        //'cache' => 'path/to/cache'    // See auto_reload option
        'debug' => true,
        'strict_variables'=> true
    ]);
    $view->addExtension(new \Slim\Views\TwigExtension(
        $c['router'],
        $c['request']->getUri()
    ));
    $view->addExtension(new \Twig_Extension_Debug());
    return $view;
};

$app->get('/config', function (Request $request, Response $response) {
    return $this->view->render($response, 'config.html',[]);
});
public function getSource($name)
{
    return file_get_contents($this->findTemplate($name));
}
我在另一个类似的服务器中使用了相同的脚本(但是,可能不同的PHP版本以及PHP.ini和httpd.conf可能不同),并且没有这个问题

显然,我配置了一些错误的东西。我应该如何配置Twig来查找模板?

是的,这是我上周遇到的一个问题(bug?),@geggleto解决了这个问题

这是因为
1.24.2
中的
v1.26.1
更新了细枝

这就是Twig在
1.24.2
中的抓取方式(方法
Twig\u Loader\u文件系统::normalizeName
):

这就是它在
1.26.1
中获取文件的方式:

private function normalizePath($path)
{
    $parts = explode('/', str_replace('\\', '/', $path));
    $isPhar = strpos($path, 'phar://') === 0;
    $new = array();
    foreach ($parts as $i => $part) {
        if ('..' === $part) {
            array_pop($new);
        } elseif ('.' !== $part && ('' !== $part || 0 === $i || $isPhar && $i < 3)) {
            $new[] = $part;
        }
    }
    return implode('/', $new);
}

总而言之:这是因为Twig的新版本。

它抱怨
points.html
,而不是
config.html
。@AlexBlex抱怨所有这些。我的错。我将编辑原始帖子。啊,很公平。然后尝试使用绝对路径。类似于
new\Slim\Views\Twig的东西(uuu DIR_uuu./../application/templates',..
@AlexBlex,这很有效。谢谢。为什么一台服务器使用相对地址,而另一台服务器却不使用?是的,如果有人能解释的话,id将是一个很好的答案。没错!我在第一台服务器上更新了composer,现在它在那里也不工作了。很高兴知道,但是,我正在得到一致的回复结果。阅读细枝变化日志,他们没有强调问题。谢谢你的全面回答。