简体   繁体   中英

Zend Framework 3 Xampp database connection

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??

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,

很简单,您在键$this有错误,而是写了$htis

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM