如何从Shopware中的后端控制器添加重定向

时间:2019-01-21 10:32:34

标签: php shopware

我已经创建了一个插件,在完成插件的其他过程之后,我想从后端的控制器重定向到给定的URL。

我已经创建了一个插件,可以根据订单创建文档,并且可以正常工作。但是,在此过程结束时,我想重定向到可以下载或打开已创建文档的URL。我知道这样做的网址是这样的(http://localhost:8000/backend/Order/openPdf?id=harshvalueforpdf)。我在本地主机上的docker中使用Shopware版本5.5.1。

 public function redirectmyurlAction()
 {
     $harsh = "9ce6b9a9cd5d469386fbb5bd692f9644";
     $search_word = $harsh;
     error_log(print_r(array('Reached redirect action'), true)."\n", 3, Shopware()->DocPath() . '/test.log');
     $this->redirect(
 array(
   'module'=> backend,
     'controller' => 'Order',
     'action' => 'openPdf?id='.$search_word,
   )
 );
 }

我希望当进程执行此操作时,用户将被重定向到创建的url,然后它应该能够下载或显示pdf。但是它会记录我在重定向之前放置的日志,但不会重定向。没有登录错误或控制台。当我在前端放置相同的重定向时,我得到了CSRFTokenValidationException,这是我所期望的,但是它显示了重定向在那起作用,所以为什么不在后端使用

更新

回复后,我已复制该函数并按如下所示对其进行了修改,但它会将所有内容记录在其中,但我仍然没有任何遗漏之处吗?

 public function openmyPdf($DocHarsh, $orderId)
    {
        error_log(print_r(array('Entered openmyPdf function',$DocHarsh,$orderId,$date), true)."\n", 3, Shopware()->DocPath() . '/error.log');

        $filesystem = $this->container->get('shopware.filesystem.private');
        $file = sprintf('documents/%s.pdf', basename($DocHarsh));

        if ($filesystem->has($file) === false) {
            error_log(print_r(array('Entered if statement, file doesnt exists ',$DocHarsh,$orderId,$date), true)."\n", 3, Shopware()->DocPath() . '/error.log');
            $this->View()->assign([
                'success' => false,
                'data' => $this->Request()->getParams(),
                'message' => 'File not exist',
            ]);

            return;
        }
        // Disable Smarty rendering
        $this->Front()->Plugins()->ViewRenderer()->setNoRender();
        $this->Front()->Plugins()->Json()->setRenderer(false);
        $orderModel = Shopware()->Models()->getRepository(Document::class)->findBy(['hash' =>$DocHarsh]);
        $orderModel = Shopware()->Models()->toArray($orderModel);
        $orderId = $orderModel[0]['documentId'];
        $response = $this->Response();
        $response->setHeader('Cache-Control', 'public');
        $response->setHeader('Content-Description', 'File Transfer');
        $response->setHeader('Content-disposition', 'attachment; filename=' . $orderId . '.pdf');
        $response->setHeader('Content-Type', 'application/pdf');
        $response->setHeader('Content-Transfer-Encoding', 'binary');
        $response->setHeader('Content-Length', $filesystem->getSize($file));
        $response->sendHeaders();
        $response->sendResponse();
        $upstream = $filesystem->readStream($file);
        $downstream = fopen('php://output', 'wb');
        while (!feof($upstream)) {
            fwrite($downstream, fread($upstream, 4096));
        }
        error_log(print_r(array('leaving the pdf function',$DocHarsh,$orderId,$upstream,$downstream), true)."\n", 3, Shopware()->DocPath() . '/error.log');
    }

2 个答案:

答案 0 :(得分:3)

请查看订单模块的后端控制器。应该是一样的情况。此功能用于从后端打开/下载文档:  https://github.com/shopware/shopware/blob/5.5/engine/Shopware/Controllers/Backend/Order.php#L1113

我认为通过下载将后端用户(从后端上下文)重定向到新的空白页可能会造成混淆。

答案 1 :(得分:0)

根据我自己的评价。我认为您遇到的问题是因为这不是一个动作,而只是一个函数,请尝试使其变为一个Action,然后像原始版本一样通过浏览器运行它。

别忘了将其列入白名单。

使用该类,使用Shopware \ Components \ CSRFWhitelistAware;

然后

类似的东西

    /**
 * {@inheritdoc}
 */
public function getWhitelistedCSRFActions()
{
    return [
        'youropenPdfActionnamewithoutthewordAction'
    ];
}

,还将实现CSRFWhitelistAware的实现添加到您的类声明中。