0

我可能对 Laminas 有一些理解问题,或者我太复杂了 :-) 希望有人能在这方面带来启示......

我有一个 IndexController、一个 IndexController Factory 和 2 个表(用户、照片)。

这些表都是 AbstractTableGateway 的扩展:

用户表.php

<?php

declare(strict_types=1);

namespace Slideshow\Model\Table;

use Laminas\Db\Adapter\Adapter;

class UserTable extends AbstractTableGateway
{
    protected $adapter;
    protected $table = 'user';

    public function __construct(Adapter $adapter)
    {
        $this->adapter = $adapter;
        $this->initialize();
    }

    public function getUsersThatHaveAGallery()
    {
         // sql... select... from... where.....
    }
}

照片表.php

<?php

declare(strict_types=1);

namespace Slideshow\Model\Table;

use Laminas\Db\Adapter\Adapter;

class UserTable extends AbstractTableGateway
{
    protected $adapter;
    protected $table = 'photos';

    public function __construct(Adapter $adapter)
    {
        $this->adapter = $adapter;
        $this->initialize();
    }

    public function getPhotosFromUser($user_id)
    {
         // sql...select photos from user where id = ....
    }
}

在我的 IndexController 中,我正在创建一个 Gallery-Class 的实例,它需要上面提到的 UserTable 和 PhotosTable。

IndexControllerFactory.php现在将 UserTable和PhotoTableIndexController.php注入到 Gallery 类中是正确的方法,如下所示:

索引控制器工厂.php

<?php

declare(strict_types=1);

namespace Slideshow\Controller\Factory;

use Slideshow\Model\Table\UserTable;
use Slideshow\Model\Table\PhotosTable;
use Interop\Container\ContainerInterface;
use Laminas\ServiceManager\Factory\FactoryInterface;


class IndexControllerFactory implements FactoryInterface
{
    public function __invoke(ContainerInterface $container, $requestName, array $options = null)
    {
        return new IndexController(
            $container->get(UserTable::class),
            $container->get(PhotosTable::class),
            $container->get('ApplicationConfig')
        );
    }
}

索引控制器.php


<?php

declare(strict_types=1);

namespace Slideshow\Controller;

use Slideshow\Model\Table\UserTable;
use Slideshow\Model\Table\PhotosTable;
use Laminas\Mvc\Controller\AbstractActionController;
use Laminas\View\Model\ViewModel;

class IndexController extends AbstractActionController
{
    private $userTable;
    private $photosTable;
    private $config;

    public function __construct(UserTable $userTable, PhotosTable $photosTable, array $config)
    {
        $this->userTable = $userTable;
        $this->photosTable = $photosTable;
        $this->config = $config;
    }

    public function indexAction()
    {
        // At this Point now I have to inject the Tables again ? 
        $Gallery = new Gallery($this->userTable, $this->photosTable);

        $html = $Gallery->renderUserListHtml();
        
        var_dump($html);
    }
}


图库.php

class Gallery {

   private $userTable;
   private $photosTable;

   // At this point I have to inject the tables AGAIN to make them usable in the Gallery.php ?
   public function __construct(UserTable $userTable, PhotosTable $photosTable)
   {
        $this->userTable = $userTable;
        $this->photosTable = $photosTable;
    {
 
   public function renderUserListHtml()
   {
      $sqlResult = $this->userTable->getUsersThatHaveAGallery();

      foreach($sqlResult as $k => $v) {
            $html .= '<div>' . $v['user_name'] . '</div><br />';
      }
 
      return $html;
}

所以我的主要问题是:上面写的代码是否正确?真的有必要注入表格吗

  1. 从工厂到 IndexController
  2. 从 IndexController 到 Gallery 类

直到我终于可以在画廊类的方法中“使用它们”。

在我终于可以使用它们之前,它似乎有很多代码!?

4

1 回答 1

0

更正确的方法是为 Gallery 类创建一个 GalleryFactory 类,将 UserTable 和 PhotosTable 注入其中,然后将 Gallery 对象注入 IndexControllerFactory 中的 IndexController

这样 IndexController 不需要处理表类。

像这样,创建一个 GalleryFactory 来创建 Gallery 对象并注入表。

<?php
namespace Slideshow\Model\Factory;

use Slideshow\Model\Gallery;
use Slideshow\Model\Table\UserTable;
use Slideshow\Model\Table\PhotosTable;
use Interop\Container\ContainerInterface;
use Laminas\ServiceManager\Factory\FactoryInterface;

class GalleryFactory implements FactoryInterface
{
     public function __invoke(ContainerInterface $container, $requestName, array $options = null)
    {
        return new Gallery(
             $container->get(UserTable::class),
             $container->get(PhotosTable::class)
        );
    }
}

然后更改 indexControllerFactory 以注入

<?php
namespace Slideshow\Controller\Factory;

use Slideshow\Model\Gallery;
use Interop\Container\ContainerInterface;
use Laminas\ServiceManager\Factory\FactoryInterface;


class IndexControllerFactory implements FactoryInterface
{
    public function __invoke(ContainerInterface $container, $requestName, array $options = null)
    {
        return new IndexController(
            $container->get(Gallery::class),
            $container->get('ApplicationConfig')
        );
    }
}

并更改 indexController 以恢复 Gallery 对象

<?php
namespace Slideshow\Controller;

use Slideshow\Model\Gallery;
use Laminas\Mvc\Controller\AbstractActionController;
use Laminas\View\Model\ViewModel;

class IndexController extends AbstractActionController
{
    private $gallary;
    private $config;

    public function __construct(Gallary $gallery, array $config)
    {
        $this->gallery= $gallery;
        $this->config = $config;
    }

    public function indexAction()
    {
        $html = $this->gallery->renderUserListHtml();
        var_dump($html);
    }
}
于 2021-02-16T14:37:51.277 回答