【问题标题】:Slim 3 direct access to a fileSlim 3 直接访问文件
【发布时间】:2016-06-02 17:56:03
【问题描述】:

我想创建一个直接访问文档的路径。 例如,我想访问domain/file/document.pdf,但通过domain/doc

我试过这个:

$app->get('/docs/v1', function ($request, $response){
    $this->view->render( $response, '/docs/document.pdf');
});

但它不起作用。我也在官方文档上看到了这个:

$app = new \Slim\App();
$app->get('/hello', function ($req, $res) {
    $this['view']->display('profile.html', [
        'name' => 'Josh',
        'url' => 'https://joshlockhart.com'
    ]);
});

但是显示功能不存在。

Fatal error: Call to undefined method Slim\Views\PhpRenderer::display() 

也许我错过了什么,我不知道,我没有在文档中找到任何其他内容。

谢谢!

【问题讨论】:

    标签: php slim slim-3


    【解决方案1】:

    这里有几点:

    1) 在 Slim v3 中,"$this['view']" 通常指向您的 Twig 渲染器。因此,您尝试调用 Twig 渲染器,在这种情况下,这可能不是您想要做的。

    2) 调用 Twig 渲染器并返回其输出的正确语法是

    return $this->view->render ($response, 'yourTemplate.twig', [ ... array of parameters goes here ...]);
    

    3)至于直接访问文档(我以前从未这样做过,所以我的回答可能是错误的),您可能希望像这样重定向到文档本身:

    return $response->withRedirect ((string)($request->getUri()->withPath('path/to/document')));
    

    让浏览器处理如何处理该文档类型。

    希望这会有所帮助。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-03-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-12-13
      • 2014-08-28
      • 1970-01-01
      相关资源
      最近更新 更多