ZF 2 - 致命错误:在null上调用成员函数getPosts()

时间:2015-06-22 16:27:17

标签: php zend-framework2 fatal-error member-functions

我正在使用Zend Framework 2创建一个网站,我以Zend Technology,Zend Framework 2:Fundamentals官方课程的练习为例。

我有一个名为posts的表,我想在我的主页中显示表格内容,按ID排序。这些是我写的代码:

Controller/PostsTableTrait.php

trait PostsTableTrait
{
    private $postsTable;

    public function setPostsTable($postsTable)
    {
        $this->postsTable = $postsTable;
    }
}

Controller/IndexController.php

class IndexController extends AbstractActionController
{
    use PostsTableTrait;

    public function indexAction()
    {
        return new ViewModel(array(
            'post' => $this->postsTable->getPosts()
        ));
    }
}

Factory/IndexControllerFactory.php

class IndexControllerFactory implements FactoryInterface
{
    public function createService(ServiceLocatorInterface $serviceLocator)
    {
        $serviceManager = $serviceLocator->getServiceLocator()->get('ServiceManager');

        $indexController = new IndexController();
        $indexController->setPostsTable($serviceManager->get('Rxe\Factory\PostsTable'));

        return $indexController;
    }
}

Factory/PostsTableFactory.php

class PostsTableFactory implements FactoryInterface
{
    public function createService(ServiceLocatorInterface $serviceLocator)
    {
        return new PostsTable(PostsTable::$tableName, $serviceLocator->get('Zend\Db\Adapter\AdapterService'));
    }
}

Model/PostsTable.php

class PostsTable extends TableGateway
{
    public static $tableName = "posts";

    public function getPosts()
    {
        $select = new Select(self::$tableName);
        $select->columns(array(
            'date',
            'title',
            'text',
            'category'
        ));
        $select->order('id DESC');

        return $select;
    }
}

config/module.config.php

'controllers' => array(
    'invokables' => array(
        'Rxe\Controller\Index' => 'Rxe\Controller\IndexController',
        'Rxe\Controller\Panel' => 'Rxe\Controller\PanelController'
    ),
    'factories' => array(
        'Rxe\Factory\PanelController' => 'Rxe\Factory\PanelControllerFactory'
    )
),
'service_manager' => array(
    'factories' => array(
        'Rxe\Factory\PanelForm' => 'Rxe\Factory\PanelFormFactory',
        'Rxe\Factory\PanelFilter' => 'Rxe\Factory\PanelFilterFactory',
        'Rxe\Factory\PostsTable' => 'Rxe\Factory\PostsTableFactory',
        'Zend\Db\Adapter\AdapterService' => 'Zend\Db\Adapter\AdapterServiceFactory'
    )
),

我不知道错误是否属于getPosts()方法。我已经尝试了许多不同的方法来返回查询,但它们都没有任何区别,甚至没有显示另一个错误。

1 个答案:

答案 0 :(得分:1)

您已将控制器注册为' invokable'。当控制器管理器创建IndexController时,它将使用IndexControllerFactory 而不使用;因此永远不会设置Rxe\Factory\PostsTable依赖项。

要解决此问题,请更新module.config.php并将索引控制器注册到您的工厂类。

'controllers' => [
    'factories' => [
        'Rxe\Controller\Index' => 'Rxe\Factory\IndexControllerFactory',
    ],
],

此外(不是错误),IndexControllerFactory使用服务管理员调用->get('ServiceManager')

你可以将它更新为这样。

class IndexControllerFactory implements FactoryInterface
{
    public function createService(ServiceLocatorInterface $controllerManager)
    {
        // @var \Zend\ServiceManager\ServiceManager
        $serviceManager = $controllerManager->getServiceLocator();

        $indexController = new IndexController();
        $indexController->setPostsTable($serviceManager->get('Rxe\Factory\PostsTable'));

        return $indexController;
    }
}