2016-10-19 2 views
0

私はZF2ベースのプロジェクトで実施している間小枝テンプレートで私のカスタムビューヘルパーを使用したい場合は、私はビューヘルパーで方法__invokeマジックメソッドを追加する必要があります。そうでない場合は、Twigテンプレートエンジンは、ビューヘルパーの__invokeメソッドを呼び出すことができないというエラーをスローします。カスタムビューヘルパーの使用中にTwigが__invokeメソッドを必要とするのはなぜですか?

これを宣言する必要がある理由を知りたいのですが、__invokeビューヘルパーのマジック機能はありますか?

答えて

1

ドキュメントを確認してください:https://framework.zend.com/manual/2.1/en/modules/zend.view.helpers.advanced-usage.html

あなたのヘルパーは、それがPhpRendererのメソッド呼び出しであるかのように、あなたもあなたのヘルパー以内__invoke()メソッドを実装する必要が呼び出されることができるようにしたい場合。

同じことが小枝レンダラを適用する必要があり、(ショートカットとして呼び出して使用して)ヘルパークラスを実行しようとしている

PHPRendererはヘルパーへのプロキシ/ショートカットとして__invokeを()を使用している場合あなたは見ることができます:

https://github.com/zendframework/zend-view/blob/master/src/Renderer/PhpRenderer.php#L389

/** 
* Overloading: proxy to helpers 
* 
* Proxies to the attached plugin manager to retrieve, return, and potentially 
* execute helpers. 
* 
* * If the helper does not define __invoke, it will be returned 
* * If the helper does define __invoke, it will be called as a functor 
* 
* @param string $method 
* @param array $argv 
* @return mixed 
*/ 
public function __call($method, $argv) 
{ 
    $plugin = $this->plugin($method); 
    if (is_callable($plugin)) { 
     return call_user_func_array($plugin, $argv); 
    } 
    return $plugin; 
} 

私たちはその下に見ることができますInfactはである、小枝レンダラがちょうど似た何かをやっていると想像します

https://github.com/ZF-Commons/ZfcTwig/blob/master/src/ZfcTwig/View/TwigRenderer.php#L83

+0

ありがとうございました! –

関連する問題