dtdr84101 2019-05-01 16:04
浏览 39
已采纳

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

I am using Zend Expressive 2 due to PHP version constraints. If I return variables in step one of pipeline (IndexAction) the variables appear just fine.

If I delegate to the next step (VerifyInputAction) and determine there is an error in the input, I need to return an error to view script. For some reason, it will not take the variables with it that I pass with the template renderer. It will still load the template, just not with the $data array variables.

I'm using Zend View as the template renderer.

My pipeline looks as follows.

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

Another problem that might be beyond the scope of this question includes, when I make it to the next Action in the pipeline, if I go to render a view script there I get this error...

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

I will do my best to provide more code examples, but due to this being an issue at work I might have some problems with that.

Thanks!

  • 写回答

1条回答 默认 最新

  • drhanjuw56233 2019-05-01 21:39
    关注

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

    An action needs to return a response. In your VerifyInputaction you don't return a response if there is no valid csv file. I'm guessing this happens in your case and the $delegate->process($request); is triggered, which probably doesn't call another action which returns a middleware.

    Looking at your code, it makes more sense to call VerifyInputaction first, check if it is a post and verify. If any of those fails, go to the next action which would be IndexAction. This could display the form with an error message. You can pass error message within the request as explained here: https://docs.zendframework.com/zend-expressive/v2/cookbook/passing-data-between-middleware/

    Pipeline:

    • VerifyInputaction -> Check POST, verify input -> redirect if success
    • IndexAction -> render template and return response

    I don't see any reason in your code why $data is not passed. My guess is that somehow the template is rendered in IndexAction which doesn't have the $data but has error set. You might check for this. The confusion is here that you render the same template in 2 different actions. Using the solution I mentioned, you only need to render it in IndexAction.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器