2017-06-21 52 views
0

我的文件結構如下:在一根樹枝模板加載js文件不能在公用文件夾

---public(Documentroot folder, where all my css,js, images loads from) 
---models 
---modules {all my modules customer, db_management e.t.c) 
---scripts 
    |---ivr_builder 
    |------ui-bootstrap-tpls-0.2.js 
--twig_templates {All my ta} 
    |---layout.twig 

在我的index.php文件,我有這樣的:

use \Psr\Http\Message\ServerRequestInterface as Request; 
    use \Psr\Http\Message\ResponseInterface as Response; 

    require '../vendor/autoload.php'; 
    spl_autoload_register(function ($classname) { 
     require "../models/" . $classname . ".php"; 
    }); 

    $config['displayErrorDetails'] = false; 
    $app = new \Slim\App(["settings" => $config]); 
    $container = $app->getContainer(); 
    // Register component on container 
    $container['view'] = function ($container) { 
     $view = new \Slim\Views\Twig(
      '/var/www/html/ivr/twig_templates', ['cache' => false]); 


     $env = $view->getEnvironment(); 
      $lexer = new Twig_Lexer($env, array(
      'tag_comment' => array('{#', '#}'), 
      'tag_block'  => array('{%', '%}'), 
      'tag_variable' => array('[[', ']]'), 
      'interpolation' => array('#{', '}'), 
     )); 
     $env->setLexer($lexer); 
     $loader = new \ Twig_Loader_Filesystem('ivr/scripts/ivr_builder/ui-bootstrap-tpls-0.13.4.js'); 
     $env->getLoader(); 



     return $view; 
    }; 

然後在我的瀏覽器,我得到這個錯誤:

Name       | Status 
---------------------------- | ------ 
angular-aside.js    | 200 
jquery.js     | 200 
ui-bootstrap-tpls-0.1.3.4.js | 500 

我layout.swig頭看起來是這樣的:

<meta http-equiv="Content-Type" content="text/html" charset="iso-8859-1"> 
<meta http-equiv="X-UA-Compatible" content="IE=edge"> 
<meta name="viewport" content="width=device-width, initial-scale=1"> 
{% if page_title is defined and page_title is not null ? page_title: 'Log in' %} 
{% endif %} 
<link rel='stylesheet' href='/css/bootstrap.css'/> 
<link rel='stylesheet' href='/js/libraries/angular-aside/angular-aside.css'/> 
<link rel="stylesheet" href="/js/libraries/angular-notify-master/styles/css/angular-notify-texture.css" id="notifyTheme"> 
<link rel='stylesheet' href='/css/ivr_builder.css'/> 
<link rel="stylesheet" type="text/css" href="/css/custom.css"> 
<script type="application/javascript" src="/js/libraries/angular.js"></script> 
<script type="application/javascript" src="/js/libraries/angular-animate.min.js"></script> 
<script type="application/javascript" src="/scripts/ivr_builder/ui-bootstrap-tpls-0.13.4.js"></script> 

<script type="application/javascript" src="/js/libraries/angular-aside/angular-aside.js"></script> 
<script type="application/javascript" src="/js/libraries/angular-notify-master/scripts/dist/angular-notify.js"></script> 
<script type="application/javascript" src="/js/jquery-ui-1.11.4/external/jquery/jquery.js"></script> 
[[top_bar]] 

請有關如何爲什麼它不能在瀏覽器中加載請任何建議。

回答

0

我可以輕鬆地使用xsendfile加載它:

發送文件 發送帶有xsendfile文件非常簡單:

<?php 
//We want to force a download box with the filename hello.txt 
header('Content-Disposition: attachment;filename=hello.txt'); 


//File is located at /home/username/hello.txt 
header('X-Sendfile: /home/username/hello.txt'); 

你可以省略第一個頭部,在這種情況下,瀏覽器將不必須顯示一個下載文件對話框。另外,X-Sendfile標題不會顯示在用戶的瀏覽器中,並且他們永遠不會看到他們收到的文件的真實位置。

您不需要發送Content-Length標頭,因爲Apache會爲您處理這個問題。也發現了這個在斯利姆:

XSendFile On 
XSendFilePath "/path/to/directory/containing/js/files" 

而在你的苗條申請途徑回調,使用絕對路徑設置X-SENDFILE頭上面指定的文件路徑中的文件:

$app->get('/get-file', function() use ($app) { 
    $res = $app->response(); 
    $res['X-SendFile'] = '/path/to/directory/containing/js/files/foo.js'; 
}); 
+0

我然而,我很驚訝我不能用slim來加載它 – Raphael

+0

所以我打算給這個鏡頭: XSendFile在 XSendFilePath「/ path/to/directory/contained/js/files」 而在你的Slim應用程序中,路由回調,請在上面指定的文件路徑中將X-SendFile標頭的絕對路徑設置爲一個文件: $ app-> get('/ get-file',function()use($ app){ $ res = $ app-> response(); $ res ['X-SendFile'] ='/path/to/directory/containing/js/files/foo.js'; }); – Raphael