setDocument equivalent in Pimcore 5

How do I render a specific document in a controller action (e.g. after forwarding)?

In Pimcore 4 I could (in an action) do:

$this->setDocument( Document::getByPath('/docname') );

however in Pimcore 5 this no longer exists and trying to call Document\Service::render reports Property “document” is a request attribute and can’t be set on the controller instance.

To answer my own post I found one way to achieve this but would still be interersted to know if there’s a better way. I created a function in my controller:

    /**
     * 
     * @param Request $request
     * @param string $path - Pimcore document full path
     * @param array $params
     * @return \Symfony\Component\HttpFoundation\Response
     */
    protected function renderDocument(Request $request, $path, $params=[])
    {
        $this->disableViewAutoRender($request);
        $document = \Pimcore\Model\Document::getByPath($path);
        $response = \Pimcore\Model\Document\Service::render($document, $params);
        return new \Symfony\Component\HttpFoundation\Response($response, 307);
    }

This works for me:

$request->attributes->set('contentDocument', $document);

in controller action ($request is injected)

Inside the Controller Action, this works, also:

return $this->render(':Namespace:mytemplate.html.php', ['document' => \Pimcore\Model\Document::getById(1)]);