2017-01-24 9 views
0

私はカスタマーコントローラーにフォームを持っています。私がしたいのは、別のコントローラのフォームを同じ顧客ページに追加することです。 SilverstripeバーでiFrameを使ってこれを行う方法はありますか?シルバーストライプ - コントローラー内にコントローラーを含める

+0

は、フォームでのネストされたコントローラの高度な例のために私のContentBlock/PageBuilderモジュールを見てみましょうええ –

答えて

1

もちろん、コードにいくつかの変更が必要な場合があります。

私はあなたの目標を達成するための考えることができる2つの主要なアプローチがあります:

1.コントローラのアクションから、フォームの作成を分離:

class Foo extends Controller { 
    private static $allowed_actions = ['FooForm', 'BarForm']; 

    public function FooForm() { 
     return new Form($this, __FUNCTION, new FieldList(), new FieldList()); 
    } 

    public function BarForm() { 
     return Bar::get_bar_form($this, __FUNCTION__); 
    } 
} 

class Bar extends Controller { 
    private static $allowed_actions = ['BarForm']; 

    public function BarForm() { 
     return static::get_bar_form($this, __FUNCTION__); 
    } 

    /** 
    * A static function that accepts the controller (Bar or Foo in this case) and a name 
    * This way, this form can easily be used on other controllers as well 
    * Just be aware that this way, the Forms controller is not always the same, so if you have a custom form that calls specific methods of the Bar controller this will not work 
    */ 
    public static function get_bar_form($controller, $name) { 
     return new Form($controller, $name, new FieldList(), new FieldList()); 
    } 
} 

2.ネストされたコントローラ:

SilverStripeでは、コントローラをネストすることができます。これは本質的にFormsがすでに行っていることです。 SilverStripeフォームはController(または、むしろRequestHandler)です。
SilverStripeでは、いずれのControllerアクションも別のRequestHandlerControllerはサブクラスであるRequestHandler)を返すことができ、処理されます。

したがって、Fooコントローラ内からバーコントローラ全体を返し、子コントローラとして実行することができます。 URLは/foo/bar/BarFormとなります。
しかし、標準のコントローラでは、ネストされたURLを持つために微妙な作業が必要になると思います。 、あなたはそのフォームのテンプレートを「含む」がある場合、あなたは別のテンプレートからのことを含めることができ
PageBuilder_Field.php#L179
PageBuilder_Field_Handler_Block.php#L32

関連する問題