0

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

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

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

我的管道如下所示。

索引操作()

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

验证输入操作()

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

可能超出此问题范围的另一个问题包括,当我进入管道中的下一个操作时,如果我在那里渲染视图脚本,我会收到此错误...

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

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

谢谢!

4

1 回答 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.

于 2019-05-01T21:39:32.750 回答