dongwu9063 2017-03-04 08:02
浏览 46

Zend Framework 3 Xampp数据库连接

I am new to Zend Framework 3 and I am doing this tutorial:

I have a xampp, mysql setup.

I have done everything exactly like in this tutorial. Now I am at the point where you configure the database connection. Further I have set up the controller and view.

In the tutorial link above , they are using php to create a database and then in config/autoload/global.php.....the following code:

return [
'db' => [
    'driver' => 'Pdo',
    'dsn'    => sprintf('sqlite:%s/data/zftutorial.db', realpath(getcwd())),
   ],
];

I have edited this to:

'db' => [
'driver' => 'Pdo_Mysql',
'dsn'    => 'mysql:dbname=dbname;host=localhost;charset=utf8;username=myuser;password=mypassword',
],

When I call the url for the index view, there the following error:

Warning: Creating default object from empty value in C:\xampp\htdocs\zendtest\module\Album\src\Controller\AlbumController.php on line 15

Fatal error: Call to a member function fetchAll() on null in C:\xampp\htdocs\zendtest\module\Album\src\Controller\AlbumController.php on line 22

The AlbumController:

 <?php

    namespace Album\Controller;

    use Album\Model\AlbumTable;
    use Zend\Mvc\Controller\AbstractActionController;
    use Zend\View\Model\ViewModel;

    class AlbumController extends AbstractActionController {

    private $table;

    public function __construct(AlbumTable $table)
    {
        $this->table = $table;
    }


    public function indexAction()
    {
        return new ViewModel([
            'albums' => $this->table->fetchAll(),
        ]);
    }

    }

I think that the connection doesn't work??

  • 写回答

2条回答 默认 最新

  • dongtao9158 2017-03-21 13:26
    关注

    can you share your "AlbumControllerFactory.php" ?

    if you have not yet created the factory you should do.

    1 - Create AlbumControllerFactory that implements FactoryInterface

    2 - Inside __invoke function use the Container to inject AlbumTable to your controller

    3 - config your mapping in module.config.php

    'controllers' => [
        'factories' => [
            Controller\AlbumController::class => Controller\Factory\AlbumControllerFactory::class,
    
    评论

报告相同问题?

悬赏问题

  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器