【问题标题】:Silverstripe - Include controller within controllerSilverstripe - 在控制器中包含控制器
【发布时间】:2017-01-24 10:41:08
【问题描述】:

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

【问题讨论】:

  • 如果您有该表单的“包含”模板,您可以从另一个模板中包含该模板,是的

标签: controller silverstripe


【解决方案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 控制器,并将其作为子控制器运行。所以 URL 可能是/foo/bar/BarForm
但是对于标准控制器,我认为您需要做一些修改才能拥有嵌套的 URL。

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-09-15
    • 2015-12-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-07-11
    相关资源
    最近更新 更多