如何从 Zend Framework 3 中的控制器调用模型方法
How to call a Method of Model from Controller in Zend Framework 3
对于没有正确地构建问题标题,我深表歉意。
我正在研究 zf3 的框架应用程序来实现 acl.I 无法弄清楚如何检索相应电子邮件的行 address.I 有两个控制器 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() 方法
LoginTable.php。但它在 non-object
上向成员函数 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;
}
您正在 AlbumController class 中调用 $this->loginTable->getRow()
,但您没有在此控制器中定义 loginTable
。您是在 LoginController class 中完成的,但这不是同一个对象。
在你的 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)
);
}
}
对于没有正确地构建问题标题,我深表歉意。 我正在研究 zf3 的框架应用程序来实现 acl.I 无法弄清楚如何检索相应电子邮件的行 address.I 有两个控制器 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() 方法 LoginTable.php。但它在 non-object
上向成员函数 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;
}
您正在 AlbumController class 中调用 $this->loginTable->getRow()
,但您没有在此控制器中定义 loginTable
。您是在 LoginController class 中完成的,但这不是同一个对象。
在你的 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)
);
}
}