0

コントローラーからカスタム ビュー ヘルパーにデータを渡す必要があります。2つの方法でやろうとしましたが、残念ながらうまくいきません。
module.config.php にヘルパーを登録しました

コントローラーからヘルパーに変数を渡そうとした最初の方法
: 私のコントローラーでは:

public function indexAction()
{  
        $this->data = $this->getApplicationTable()->getTypes();

        $helper = new TestHelper();
        $helper->setVariables($this->data);
}

これが私のヘルパーです:

class TestHelper extends AbstractHelper {

    public $data;


    public function __invoke()
    {
        var_dump($this->data); // output null
        return $this->getView()->render('helper-view.phtml', $this->data);
    }



    public function setVariables($var)
    {
        if($var){
            $this->data = $var;
            var_dump($this->data) // output array with correct data
        }
    }



}

レイアウトでは、次のように表示します。

<?php echo $this->testHelper(); ?>

そして、変数が空であるという helper-view.phtml からエラーが発生しました。

私が試した2番目の方法は、依存性注入に基づいています

私のmodule.php:

public function getServiceConfig()
    {
        return array(
            'factories' => array(
                'Application\Model\ApplicationTable' =>  function($sm) {
                    $tableGateway = $sm->get('ApplicationTableGateway');
                    $table = new ApplicationTable($tableGateway);
                    return $table;
                },
                'ApplicationTableGateway' => function ($sm) {
                    $dbAdapter = $sm->get('Zend\Db\Adapter\Adapter');
                    return new TableGateway('tableName', $dbAdapter);
                },
            ),
        );
    }

public function getViewHelperConfig()
{
    return array(
        'factories' => array(
            'TestHelper' => function ($helperPluginManager) {
                $sm = $helperPluginManager->getServiceLocator();
                $tableGateway = $sm->get('Application\Model\ApplicationTable');
                $viewHelper = new TestHelper();
                $viewHelper->setTableGateway($tableGateway);
                return $viewHelper;
            }
        ),
    );
}

私のヘルパー:

class TestHelper extends AbstractHelper {

    public $tableGateway;


    public function __invoke()
    {
        $data = $this->tableGateway->getTypes();

        return $this->getView()->render('helper-view.phtml', $data);
    }



    public function setTableGateway($tableGateway)
    {
        $this->tableGateway = $tableGateway;
    }


}

最初の方法と同じエラーが発生しました。
どんな助けにも感謝します。

4

2 に答える 2

1

依存性注入でこれを行う場合は、構築中に tableGateway をパラメーターとして渡す必要があります。それ以外の場合は、呼び出し中に渡すことができます。後者の場合、可能であれば、次のようにします。

 class TestHelper extends AbstractHelper {

     public function __invoke($tableGateway)
     {
         $data = $tableGateway->getTypes();

         return $this->getView()->render('helper-view.phtml', $data);
     }

そしてレイアウトで:

<?php echo $this->testHelper($tableGateway); ?>

または、コンストラクターを介して挿入するアプローチの場合:

public function getViewHelperConfig()
{
    return array(
        'factories' => array(
            'TestHelper' => function ($helperPluginManager) {
                $sm = $helperPluginManager->getServiceLocator();
                $tableGateway = $sm->get('Application\Model\ApplicationTable');
                $viewHelper = new TestHelper($tableGateway);
                return $viewHelper;
            }
        ),
    );
}

class TestHelper extends AbstractHelper {
    protected $tableGateway;        

    public function __construct($tableGateway)
    {
        $this->tableGateway = $tableGateway;
    }

    public function __invoke()
    {
        $data = $this->tableGateway->getTypes();
        return $this->getView()->render('helper-view.phtml', $data);
    }

}

そしてレイアウトで:

<?php echo $this->testHelper(); ?>

これがあなたの状況で機能することを確認するのに十分な情報やコードはありませんが、http://zendblog.shinymayhem.com/2013/09/using-servicemanager-as-inversion-ofを確認できますファクトリと依存性注入の詳細については、.htmlを参照してください。

于 2013-09-11T22:48:33.033 に答える
1

$this->testHelper(); を呼び出すため、機能しません。$data を認識しない TestHelper の新しいインスタンスを作成します。このインスタンスに対して setVariables が呼び出されることはありませんでした。また、ヘルパー内でビューを返さないので、可能であればこれをコントローラーに残します。ヘルパーは、ビューのヘルパー メソッドを提供するために使用されます。次のドキュメントを参照してください。ヘルパー

簡単な解決策、DI なしでこれを行う方法: コントローラーから必要なデータをビューに渡して、必要に応じてビューがデータをヘルパーに渡すことができるようにします。

indexAction 内で、データ変数を設定し、その変数をビューに渡します。

public function indexAction() {
    $data = $this->getApplicationTable()->getTypes();
    return new \Zend\View\Model\ViewModel(array(
        'data' => $data,
    ));
}

ヘルパーを変更します。setVariables メソッドを削除し、invoke $data パラメータの署名を追加します。ところで、変数をプライベートに設定し、セッターとゲッターを追加することをお勧めします。

class TestHelper extends AbstractHelper {

    private $_data;

    public function __invoke($data) {
        $this->setData($data);
        return var_dump($this->getData());
    }

    public function setData($data) {
        $this->_data = $data;
    }

    public function getData() {
        return $this->_data;
    }

}

これで、index.phtml でコントローラーから $data を取得し、それをヘルパーに渡すことができます。

$data = $this->data;
echo $this->testHelper($data);

それで全部です。

于 2013-07-22T12:11:53.960 に答える