由于 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
我会尽力提供更多代码示例,但由于这是工作中的一个问题,我可能会遇到一些问题。
谢谢!