1

我想制作一条直接访问文档的路线。例如,我想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() 

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

谢谢!

4

1 回答 1

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')));

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

希望这可以帮助。

于 2016-06-02T09:56:03.810 回答