到目前为止,一切都完美无缺。我在页面上:http://framework.zend.com/manual/current/en/in-depth-guide/understanding-routing.html。
在此页面上,我必须修改3个文件:
-module.config.php
-detail.phtml
-ListController.php
我收到以下错误:
发布详情
帖子标题
致命错误:在C:\ Program Files \ xampp \ htdocs \ path \ to \ zf2-tutorial \ module \ Blog \ view \ blog \ list \ detail.phtml上调用null上的成员函数getTitle() 6
我没有粘贴代码,因为它与链接相同。你能帮我解决一下我的问题吗?
public function detailAction()
{
$id = $this->params()->fromRoute('id');
try {
$post = $this->postService->findPost($id);
} catch (\InvalidArgumentException $ex) {
return $this->redirect()->toRoute('blog');
}
return new ViewModel(array(
'post' => $post
));
}
答案 0 :(得分:1)
感谢您的更新。既然我在教程中看到了你的位置,我认为你在Mapper中遇到了问题。请参阅上一页和Finishing the Mapper
章节如果您的映射器找不到文章,它应该抛出错误,如第63行的代码示例所示。显然,您的映射器返回null
,这会导致您看到错误Call to a member function getTitle() on null
。因为null毕竟不是一个对象,并且没有getTitle()函数。
因此,请查看ZendDbSqlMapper
类和find($id)
方法,并确保在找不到ID时抛出错误。