ZendFramework 2 教程 Post Title:getTitle() 致命错误

ZendFramework 2 tutorial Post Title:getTitle() Fatal Error

到目前为止一切正常。我在页面上:http://framework.zend.com/manual/current/en/in-depth-guide/understanding-routing.html.

在这个页面上我不得不修改 3 个文件:

-module.config.php

-detail.phtml

-ListController.php

我收到以下错误:

Post Details

Post Title

Fatal error: Call to a member function getTitle() on null in C:\Program Files\xampp\htdocs\path\to\zf2-tutorial\module\Blog\view\blog\list\detail.phtml on line 6

我没有粘贴代码,因为它与 link 中的相同。你们能帮我解决我的问题吗?

public 函数 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
    ));
}

感谢更新。现在我看到你在教程中的位置,我认为你在映射器中有问题。请参阅上一页和上一章 Finishing the Mapper

如果您的映射器找不到文章,它应该会抛出一个错误,如第 63 行的代码示例所示。显然,您的映射器 returns null 会导致您看到的错误 Call to a member function getTitle() on null.因为 null 毕竟不是对象,也没有 getTitle() 函数。

因此请查看 ZendDbSqlMapper class 和 find($id) 方法,并确保在找不到 id 时抛出错误。