简体   繁体   English

Slim应用程序错误类型-Twig_Error_Loader

[英]Slim Application Error type- Twig_Error_Loader

I am newbie to slim framework and working on a live video streaming app. 我是苗条框架的新手,正在开发实时视频流应用程序。 This one is my first ever question i am adding to stack overflow account. 这是我要添加到堆栈溢出帐户的第一个问题。 I have installed application and added all the necessary requirement dependencies with composer. 我已经安装了应用程序,并使用composer添加了所有必需的需求依赖项。 I am facing following issue when i am opening application on xampp localhost 我在xampp localhost上打开应用程序时遇到以下问题

Error: 错误:

Type: Twig_Error_Loader
Message: Unable to find template "C:/xampp/htdocs/local/dev/templates/routes/home/home.twig" (looked into: ./templates).
File: C:\xampp\htdocs\local\dev\vendor\twig\twig\lib\Twig\Loader\Filesystem.php
Line: 215

Trace
#0     C:\xampp\htdocs\local\dev\vendor\twig\twig\lib\Twig\Loader\Filesystem.php(139): Twig_Loader_Filesystem->findTemplate('C:\\xampp\\htdocs...')
#1 C:\xampp\htdocs\local\dev\vendor\twig\twig\lib\Twig\Environment.php(264): Twig_Loader_Filesystem->getCacheKey('C:\\xampp\\htdocs...')
#2 C:\xampp\htdocs\local\dev\vendor\twig\twig\lib\Twig\Environment.php(322): Twig_Environment->getTemplateClass('C:\\xampp\\htdocs...', NULL)
#3 C:\xampp\htdocs\local\dev\vendor\slim\views\Twig.php(87): Twig_Environment->loadTemplate('C:\\xampp\\htdocs...')
#4 C:\xampp\htdocs\local\dev\vendor\slim\slim\Slim\View.php(255): Slim\Views\Twig->render('C:\\xampp\\htdocs...', NULL)
#5 C:\xampp\htdocs\local\dev\vendor\slim\slim\Slim\View.php(243): Slim\View->fetch('C:\\xampp\\htdocs...', NULL)
#6 C:\xampp\htdocs\local\dev\vendor\slim\slim\Slim\Slim.php(757): Slim\View->display('C:\\xampp\\htdocs...')
#7 C:\xampp\htdocs\local\dev\templates\routes\home\home.php(16): Slim\Slim->render('C:\\xampp\\htdocs...', Array)
#8 [internal function]: {closure}()
#9 C:\xampp\htdocs\local\dev\vendor\slim\slim\Slim\Route.php(468): call_user_func_array(Object(Closure), Array)
#10 C:\xampp\htdocs\local\dev\vendor\slim\slim\Slim\Slim.php(1357): Slim\Route->dispatch()
#11 C:\xampp\htdocs\local\dev\vendor\slim\slim\Slim\Middleware\Flash.php(85): Slim\Slim->call()
#12 C:\xampp\htdocs\local\dev\vendor\slim\slim\Slim\Middleware\MethodOverride.php(92): Slim\Middleware\Flash->call()
#13     C:\xampp\htdocs\local\dev\vendor\slim\slim\Slim\Middleware\PrettyExceptions.php( 67): Slim\Middleware\MethodOverride->call()
#14 C:\xampp\htdocs\local\dev\vendor\slim\slim\Slim\Slim.php(1302):     Slim\Middleware\PrettyExceptions->call()
#15 C:\xampp\htdocs\local\dev\index.php(12): Slim\Slim->run()
#16 {main}

I am not getting what actually the error is. 我没有得到真正的错误是什么。 My site is secured with https on localhost as that was the requirement of my client. 我的站点在本地主机上使用https保护,因为这是我的客户端的要求。 I have installed SSL certificate for my xampp projects but still on https too this problem arises. 我已经为我的xampp项目安装了SSL证书,但仍然在https上也会出现此问题。

My xampp version v3.2.2 and php 5.6.19 我的xampp版本v3.2.2和php 5.6.19

Do anyone have faced the same issue before. 有没有人以前遇到过同样的问题。 Thanks in advance for quick response. 在此先感谢您的快速回复。

update : 更新 文件夹结构

update 2 更新2

this is my config file i use the slim 这是我的配置文件,我使用苗条

 $app = new \Slim\Slim([
        'view' => new \Slim\Views\Twig()
 ]);
 $view = $app->view();
 $view->parserExtensions = [new \Slim\Views\TwigExtension()];
 $mongo = new MongoClient();
 $app->db = $mongo->local;
 $app->device = new Mobile_Detect;

Update3 UPDATE3

$app->get('/', function() use ($app){
$path = templatePath(__FILE__);
$templateData = [
    'pageTitle' => 'Home',
    'UI' => [
        'noSidebar' => true
    ]
];
$app->render("$path/home.twig", $templateData);
})->name('home');
//templatepath function that returns the path to $path variable
function templatePath($file) {
return str_replace(getcwd().'/templates/', '', dirname($file));
}

From what I can tell from the traceback, it can not find the template. 从回溯中可以看出,它找不到模板。 I think the reason for that is because all paths are relative to the path that was passed to the Twig constructor. 我认为其原因是因为所有路径都相对于传递给Twig构造函数的路径。

For example, if I passed templates/views when I instantiated Twig, and my Twig file was located at templates/views/home.twig , all I would have to do is $app->render("home.twig"); 例如,如果我在实例化Twig时传递了templates/views ,而我的Twig文件位于templates/views/home.twig ,那么我要做的就是$app->render("home.twig"); .

Update 更新

Looking through the projects I've done with Slim 2, it looks like when you instantiate Slim, you can pass another option that specifies the root of where Twig will look for templates. 浏览我使用Slim 2完成的项目,看起来在实例化Slim时,您可以传递另一个选项,该选项指定Twig查找模板的位置的root So consider the following code: 因此,请考虑以下代码:

$app = new Slim([
    "view" => new Twig(),
    "templates.path" => "path/to/views/folder",
]);

After doing that you then be able to as I stated above. 完成之后,您便可以按照我上面所述的方法进行操作了。

Update 2 更新2

Rereading the error message you posted, I noticed that it is looking at ./templates . 重新阅读您发布的错误消息后,我注意到它正在查看./templates Now assuming that /templates is the same templates folder posted in the question, you could use the relative path to the twig file, which would in this instance be routes/home/home.twig . 现在假设/templates是问题中发布的相同templates文件夹,则可以使用routes/home/home.twig文件的相对路径,在这种情况下,该路径为routes/home/home.twig Now anywhere you are trying to render a twig file would just have to do $app->render("routes/path/file.twig"); 现在,您要渲染树枝文件的任何地方都只需执行$app->render("routes/path/file.twig"); , of course editing the path to the file you actually want. ,当然可以编辑您实际想要的文件的路径。

Update 3 更新3

Just for completion, the final code would look something like this: 仅为了完成,最终代码如下所示:

$app = new \Slim\Slim([
    "view" => new \Slim\Views\Twig(),
    "templates.path" => "/../templates/routes"
 ]);
$view = $app->view();
$view->parserExtensions = [
    new \Slim\Views\TwigExtension()
 ];
$mongo = new MongoClient();
$app->db = $mongo->local;
$app->device = new Mobile_Detect;

Then anywhere you would like to render a view you would do this: 然后,在任何您想要渲染视图的地方都可以这样做:

$app->render("home/home.twig");

Edit 1 编辑1

In response to the comment about a template function. 回应有关模板功能的评论。 Taking a look at your directory structure, there isn't really a need for a function of you just combine the code above and then get the filename from basename(__FILE__) . 看一下目录结构,您实际上不需要功能,只需组合上面的代码,然后从basename(__FILE__)获取文件名即可。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM