zf2 getServiceLocator()返回null

时间:2014-04-16 03:57:48

标签: php zend-framework2

控制器,getServiceLocator返回null。

namespace Test\Controller;
use Zend\Mvc\Controller\AbstractActionController;
use Zend\Mvc\Controller;
use Test\Model\Test;

class TestController extends AbstractActionController
{
    private $db;
    private function get_db()
    {
        if (!$this->db) {
           $this->db = $this->getServiceLocator()->get('db1');
        }
        return $this->db;
    }
}

/config/autoload/databae.php

return array(
    'db' => array(
        'adapters' => array(
            'db1' => array(
                'hostname' => '127.0.0.1',
                'driver' => 'mysqli',
                'port' => '3308',
                'database' => 'test',
                'username' => 'test',
                'password' => 'test',
                'charset' => 'utf8',
                'options' => array(
                    'buffer_results' => true
                )
            ),
            'db2' => array(
                'hostname' => '127.0.0.1',
                'driver' => 'pdo',
                'database' => 'test',
                'username' => 'root',
                'password' => '',
                'charset' => 'utf8',
                'options' => array(
                    PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES \'UTF8\''
                )
            )
        )
    ),
    'service_manager' => array(
        'abstract_factories' => array(
            'Zend\Db\Adapter\AdapterAbstractServiceFactory',
        )
    ),

结果

  

致命错误:在非对象中调用成员函数get()   /home/test/zf2/module/Test/src/Test/Controller/TestController.php on   第16行

0 个答案:

没有答案