2016-09-23 19 views
0

質問のタイトルを正確にフレーミングしていないことをお詫び申し上げます。私は電子メールaddress.Iの対応の行を取得する方法を見つけ出すことができませんでしたacl.Iを実装するためにZF3のスケルトンアプリケーションに取り組んでいます は二つのコントローラAlbumController.phpとLoginController.php AlbumController.phpZend 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; 
} 

私はモデル LoginTable.phpでLoginTable.phpの存在の場合のgetRow()メソッドを呼び出しています。しかし、それはあなたがあなたのAlbumControllerクラスの$this->loginTable->getRow()を呼び出しているが、あなたは」didnの非オブジェクト

LoginTable.php

上)メンバ関数のgetRow(へ
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

をエラーコールを投げていますこのコントローラーでloginTableを定義してください。 LoginControllerクラスでそれを行いましたが、これは同じオブジェクトではありません。

あなたAlbumControllerLoginTableインスタンスを注入:

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 for this info.Iセッション変数を使用しています。 –

+0

@AI Fonceは、この工場をどこで作成できますか、どのようにこの工場を呼び出すことができますか? –

+0

@AzharAhmadあなたの答えは公式の文書にあります:https://docs.zendframework.com/tutorials/in-depth-guide/models-and-servicemanager/#writing-a-factory-class –

関連する問題