【问题标题】:How to call a Method of Model from Controller in Zend Framework 3如何在 Zend Framework 3 中从控制器调用模型的方法
【发布时间】:2016-09-23 12:19:08
【问题描述】:

对于没有正确构建问题标题,我深表歉意。 我正在研究 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');
     }
   }

LoginController.php

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

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

LoginTable.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;
}

【问题讨论】:

    标签: php zend-framework zend-db zend-framework3


    【解决方案1】:

    您在 AlbumController 类中调用 $this->loginTable->getRow(),但您没有在此控制器中定义 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)
            );
        }
    }
    

    【讨论】:

    • @AI Fonce Thanx 获取信息。我使用会话变量得到它
    • @AI Fonce 你能告诉我我在哪里可以创建这个工厂吗?我怎么称呼这个工厂?
    • @AzharAhmad 你的答案在官方文档中:阅读它docs.zendframework.com/tutorials/in-depth-guide/…
    • @AI Fonce 实际上我将这个工厂调用到相册模块的 module.config.php 中。我将 AlbumTable 类实例注入到 AlbumController 中,但出现错误“无法找到服务 Album\Model\AlbumTable。我在这家工厂创造的。你能解决我的问题吗。谢谢。
    猜你喜欢
    • 2013-04-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-06-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多