简体   繁体   中英

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. I am facing following issue when i am opening application on 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. I have installed SSL certificate for my xampp projects but still on https too this problem arises.

My xampp version v3.2.2 and php 5.6.19

Do anyone have faced the same issue before. Thanks in advance for quick response.

update : 文件夹结构

update 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

$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.

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"); .

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. 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

Rereading the error message you posted, I noticed that it is looking at ./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 . Now anywhere you are trying to render a twig file would just have to do $app->render("routes/path/file.twig"); , of course editing the path to the file you actually want.

Update 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

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__) .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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