Silverstripe - 在控制器中包含控制器

时间:2017-01-24 10:41:08

标签: controller silverstripe

我有一个客户控制器,上面有一个表格。我想要做的是将另一个控制器的表单添加到同一个客户页面。有没有办法在使用iFrame的Silverstripe栏中执行此操作?

1 个答案:

答案 0 :(得分:1)

嗯,是的,但您可能需要对代码进行一些修改。

我可以想到两种主要方法来实现您的目标:

<强> 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操作都可以返回另一个RequestHandlerControllerRequestHandler的子类),然后将对其进行处理。

因此,您可以从Foo控制器中返回整个Bar控制器,并让它作为子控制器运行。因此,网址可能为/foo/bar/BarForm 但是对于标准的控制器,我认为你需要做一些修改以获得嵌套的URL。

查看我的ContentBlock/PageBuilder模块,了解带有Forms的嵌套控制器的高级示例:
PageBuilder_Field.php#L179
PageBuilder_Field_Handler_Block.php#L32