Zend Expressive没有将变量传递给查看脚本

问题描述 投票:0回答:1

由于PHP版本的限制,我正在使用Zend Expressive 2。如果我在管道(IndexAction)的第一步中返回变量,则变量看起来很好。

如果我委托下一步(VerifyInputAction)并确定输入中有错误,我需要返回一个错误来查看脚本。出于某种原因,我不会使用模板渲染器传递的变量。它仍将加载模板,而不是加载$ data数组变量。

我正在使用Zend View作为模板渲染器。

我的管道如下所示。

的indexAction()

    public function process(ServerRequestInterface $request, DelegateInterface $delegate)
    {
        if ($request->getMethod() !== "POST") {
            return new HtmlResponse($this->template->render('app::home-page', ['error' => 'hello']));
        } else {
            $delegate->process($request);
            //return new HtmlResponse($this->template->render('app::home-page'));
        }
    }

VerifyInputaction()

    public function process(ServerRequestInterface $request, DelegateInterface $delegate)
    {
        $data = [];

        $file = $request->getUploadedFiles()['recordsFile'];

        $fileType = substr($file->getClientFilename(), strpos($file->getClientFilename(), '.'));

        // If file type does not match appropriate content-type or does not have .csv extension return error
        if (! in_array($file->getClientMediaType(), $this->contentTypes) || ! in_array($fileType, $this->extensions)) {
            $data['error']['fileType'] = 'Error: Please provide a valid file type.';
            return new HtmlResponse($this->template->render('app::home-page', $data));
        }

        $delegate->process($request);
    }

另一个可能超出此问题范围的问题包括,当我将其转到管道中的下一个Action时,如果我去渲染一个视图脚本,我会收到此错误...

Last middleware executed did not return a response. Method: POST Path: /<--path-->/ .Handler: Zend\Expressive\Middleware\LazyLoadingMiddleware

我将尽我所能提供更多代码示例,但由于这是一个工作中的问题,我可能会遇到一些问题。

谢谢!

php zend-framework zend-view zend-expressive
1个回答
0
投票

最后执行的中间件没有返回响应。方法:POST路径:/ < - path - > / .Handler:Zend \ Expressive \ Middleware \ LazyLoadingMiddleware

操作需要返回响应。在VerifyInputaction中,如果没有有效的csv文件,则不会返回响应。我猜这种情况发生在你的情况下并且$delegate->process($request);被触发,这可能不会调用另一个返回中间件的动作。

看看你的代码,首先调用VerifyInputaction更有意义,检查它是否是一个帖子并验证。如果其中任何一个失败,请转到IndexAction的下一个操作。这可能会显示带有错误消息的表单。您可以在请求中传递错误消息,如下所述:https://docs.zendframework.com/zend-expressive/v2/cookbook/passing-data-between-middleware/

管道:

  • VerifyInputaction - >检查POST,验证输入 - >重定向,如果成功
  • IndexAction - >渲染模板和返回响应

我在代码中没有看到任何原因导致$ data未通过。我的猜测是,不知何故模板在IndexAction中呈现,它没有$ data但是设置了error。你可以检查一下。这里的混淆是你在2个不同的动作中渲染相同的模板。使用我提到的解决方案,您只需要在IndexAction中呈现它。

© www.soinside.com 2019 - 2024. All rights reserved.