[英]Zend Expressive not passing variables to view script
I am using Zend Expressive 2 due to PHP version constraints. 由于PHP版本的限制,我正在使用Zend Expressive 2。 If I return variables in step one of pipeline (IndexAction) the variables appear just fine.
如果我在管道(IndexAction)的第一步中返回变量,则变量看起来很好。
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. 如果我委托下一步(VerifyInputAction)并确定输入中有错误,我需要返回一个错误来查看脚本。 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.
它仍将加载模板,而不是加载$ data数组变量。
I'm using Zend View as the template renderer. 我正在使用Zend View作为模板渲染器。
My pipeline looks as follows. 我的管道如下所示。
IndexAction() 的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() 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... 另一个可能超出此问题范围的问题包括,当我将其转到管道中的下一个Action时,如果我去渲染一个视图脚本,我会收到此错误...
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! 谢谢!
Last middleware executed did not return a response.
最后执行的中间件没有返回响应。 Method: POST Path: /<--path-->/ .Handler: Zend\\Expressive\\Middleware\\LazyLoadingMiddleware
方法:POST路径:/ < - 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. 在
VerifyInputaction
,如果没有有效的csv文件,则不会返回响应。 I'm guessing this happens in your case and the $delegate->process($request);
我猜这种情况发生在你的情况下和
$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. 查看代码,首先调用
VerifyInputaction
更有意义,检查它是否是帖子并验证。 If any of those fails, go to the next action which would be IndexAction
. 如果其中任何一个失败,请转到下一个将是
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/
您可以在请求中传递错误消息,如下所述: https : //docs.zendframework.com/zend-expressive/v2/cookbook/passing-data-between-middleware/
Pipeline: 管道:
I don't see any reason in your code why $data is not passed. 我在代码中没有看到任何原因导致$ data未通过。 My guess is that somehow the template is rendered in
IndexAction
which doesn't have the $data but has error
set. 我的猜测是,不知何故,模板在
IndexAction
中呈现,它没有$ data但是有error
集。 You might check for this. 你可以检查一下。 The confusion is here that you render the same template in 2 different actions.
这里的混淆是你在2个不同的动作中渲染相同的模板。 Using the solution I mentioned, you only need to render it in
IndexAction
. 使用我提到的解决方案,您只需要在
IndexAction
呈现它。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.