2016-09-23 4 views
0

Приносим извинения за неправильное оформление вопроса. Я работаю на скелет Применение zf3 для реализации acl.I не мог понять, как получить ряд соответствующих электронной address.I два контроллера AlbumController.php и LoginController.php AlbumController.phpКак назвать метод модели из контроллера в Zend Framework 3

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

Я зову GetRow() метод LoginTable.php присутствующих в модели LoginTable.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; 
} 

ответ

1

Вы звоните $this->loginTable->getRow() в вашем AlbumController класса, но вы Ждут» t определите loginTable в этом контроллере. Вы сделали это в своем LoginController, но это не те же объекты.

Подайте экземпляр LoginTable в вашем AlbumController:

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) 
     ); 
    } 
} 
+0

@AI Fonce Thanx для info.I получил его с использованием переменной сеанса –

+0

@AI Fonce вы могли бы рассказать мне, где я могу создать эту фабрику и как я могу назвать эту фабрику? –

+0

@AzharAhmad Ваш ответ в официальной документации: прочитайте его https://docs.zendframework.com/tutorials/in-depth-guide/models-and-servicemanager/#writing-a-factory-class –

 Смежные вопросы

  • Нет связанных вопросов^_^