0

我为没有正确构建问题标题而道歉。我正在研究 zf3 的骨架应用程序来实现 acl。我不知道如何检索相应电子邮件地址的行。我有两个控制器 AlbumController.php 和 LoginController.php AlbumController.php

private $table;
public function __construct(AlbumTable $table)
{
    $this->table = $table;
}
public function deleteAction()
{
    $user_session=new Container('user');
    if(isset($user_session->email))
    {
      $row=$this->loginTable->getRow($user_session->email);//*Here is the problem*
        if($row['role']=='admin')
        {
            $acl=new Acl();
            if($acl->isAllowed('admin','AlbumController','delete'))
            {
                 $id = (int) $this->params()->fromRoute('id', 0);
                if (!$id) {
                    return $this->redirect()->toRoute('album');
                }
            $request = $this->getRequest();
            if ($request->isPost()) {
                $del = $request->getPost('del', 'No');

                if ($del == 'Yes') {
                    $id = (int) $request->getPost('id');
                    $this->table->deleteAlbum($id);
                }
                return $this->redirect()->toRoute('album');
            }
            return [
                'id'    => $id,
                'album' => $this->table->getAlbum($id),
            ];
    }
        }
     return $this->redirect()->toRoute('login');
     }
   }

登录控制器.php

public $user_session;
public $loginTable;
public function __construct(LoginTable $loginTable)
{
 $this->loginTable = $loginTable;
}

我正在调用模型 LoginTable.php 中存在的 LoginTable.php 的 getRow() 方法。但它抛出一个错误调用非对象上的成员函数 getRow()

登录表.php

class LoginTable
{
protected $tableGateway;
public function __construct(TableGateway $tableGateway)
 {
     $this->tableGateway = $tableGateway;
 }
public function getRow($mail)
{
     $email  =  $mail;
     $rowset = $this->tableGateway->select(array('email' => $email));
     $row = $rowset->current();
     if (!$row) {
         throw new \Exception("Could not find row $email");
     }
     return $row;
}
4

1 回答 1

1

$this->loginTable->getRow()您正在调用AlbumController类,但您没有loginTable在此控制器中定义。您是在LoginController类中完成的,但这不是同一个对象。

在AlbumController中注入LoginTable实例:

AlbumController.php

....

private $albumTable;
private $loginTable;

public function __construct(AlbumTable $albumTable, LoginTable $loginTable)
{
    $this->albumTable= $albumTable;
    $this->loginTable= $loginTable;
}

....

AlbumControllerFactory.php(适应您的代码):

class AlbumControllerFactory implements FactoryInterface
{
    public function __invoke(ContainerInterface $container, $requestedName, array $options = null)
    {
        return new AlbumController(
            $container->get(AlbumTable::class),
            $container->get(LoginTable::class)
        );
    }
}
于 2016-09-23T20:24:42.610 回答