简体   繁体   English

Twig无法加载资产

[英]Twig is not able to load assets

I'm trying slim with slim-extras and twig. 我正在尝试使用细长的细枝和细树枝。 Problem is that I can't include css or js files into twig html. 问题是我不能将CSS或JS文件包含在树枝HTML中。 Accessing css and js file directly from browser give 404 not found error as well as browser console throws 404 page error. 直接从浏览器访问css和js文件会导致404 not found错误以及浏览器控制台抛出404页面错误。

here is my httdocs(folder structure) 这是我的httdocs(文件夹结构)

在此处输入图片说明

My apache document root is /var/www/devdomain.dev/public and DirectoryIndex index.php 我的Apache文档根目录是/var/www/devdomain.dev/publicDirectoryIndex index.php

this is my index.php in public folder 这是我在公用文件夹中的index.php

here I tried to add hook with $app->hook and pass it to twig also did not work. 在这里,我试图用$ app-> hook添加钩子并将其传递给twig也没有用。

<?php

require '../vendor/autoload.php';

// Prepare app
$app = new \Slim\Slim(array(
    'templates.path' => '../templates',
));

// Create monolog logger and store logger in container as singleton 
// (Singleton resources retrieve the same log resource definition each time)
$app->container->singleton('log', function () {
    $log = new \Monolog\Logger('slim-skeleton');
    $log->pushHandler(new \Monolog\Handler\StreamHandler('../logs/app.log', \Monolog\Logger::DEBUG));
    return $log;
});

$app->hook('slim.before', function () use ($app) {
    $app->view()->appendData(array('baseUrl' => 'templates/'));
});

// Prepare view
$app->view(new \Slim\Views\Twig());
$app->view->parserOptions = array(
    'charset' => 'utf-8',
    'cache' => realpath('../templates/cache'),
    'auto_reload' => true,
    'strict_variables' => false,
    'autoescape' => true
);
$app->view->parserExtensions = array(new \Slim\Views\TwigExtension());


// Define routes
$app->get('/', function () use ($app) {
    // Sample log message
    $app->log->info("Home-Page '/' route");
    // Render index view
    $app->render('index.phtml');
});

// Run app
$app->run();

So my question is: how to pass template path or asset path to twig with slim? 所以我的问题是:如何将模板路径或资产路径传递给苗条的树枝?

edit: If css or js files in public folder they can be included. 编辑:如果公共文件夹中的css或js文件可以包含在内。

In my opinion it is about your setup. 我认为这与您的设置有关。 Your Browser is on index.php and from this he will search for your assets but can't find them - because they are not on your vhost . 您的浏览器位于index.php并且他将从中搜索您的资产,但找不到它们-因为它们不在您的vhost

Right physically they are there but when apache starts in the public folder your browser can not get out of there. 实际上,它们在那里,但是当apache在公用文件夹中启动时,您的浏览器就无法离开那里。 That assets have to be public (or directly "echo-ed" into your template). 该资产必须是公开的(或直接“回显”到您的模板中)。

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

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