【问题标题】:Dynamic formConfig in same controller in October CMS十月 CMS 中同一控制器中的动态 formConfig
【发布时间】:2019-10-12 20:36:16
【问题描述】:

我正在尝试建立一家汽车租赁公司,并且我希望能够在后端设置费率。但是,有两种不同(但相关)的方法可以通过单个日期或批量方式来执行此操作,方法是选择日期范围并循环遍历各个日期。

在控制器中,我定义了两个操作来执行此操作,分别是 calendar()bulk()。我还通过设置 $formConfig 公共属性来选择要加载的表单字段 yaml 文件。我的控制器看起来像这样:

class AvailableCars extends Controller
{
    public $formConfig = 'config_form.yaml';

    public function bulk($recordId = null, $context = null)
    {
        $this->pageTitle = "Bulk update rates";
        $model = $this->formFindModelObject($recordId);
        $this->initForm($model);
    }

    public function calendar($recordId = null, $context = null)
    {
        $this->pageTitle = "Update rates for single date on calendar";
        $model = $this->formFindModelObject($recordId);
        $this->initForm($model);
    }

    public function onSave($recordId = null, $context = null)
    {
        $this->formFindModelObject($recordId)->save();
        Flash::success("Rates saved successfully");
    }
}

问题是这适用于一个的动作,但是如果我输入,例如:

$this->formConfig = 'alternate_fields.yaml';

bulk()calendar() 方法中,它确实 覆盖行为并加载不同的表单配置yaml 文件;如果它以前没有定义为类属性,它甚至会出错。所以我可以假设这个 yaml 文件是在 调用这些方法中的任何一个之前加载的。

所以我的问题是,有没有办法根据入口点加载动态formConfig yaml 文件?或者,这在 Laravel/October 中是不是很好,或者每个控制器应该只负责做一件事,并且只有一种方法来创建/读取/更新/销毁模型?

【问题讨论】:

    标签: php laravel octobercms octobercms-backend


    【解决方案1】:

    您可以根据需要手动设置配置文件。但是我们还需要遵循一些规则并为其添加具有适当命名约定的所需方法。 是的,如果我们做得好,每一件事都是好的做法:)

    您可以将此代码用于update existing records

    public function updatebulk($recordId = null, $context = null)
    {
        $this->pageTitle = "Bulk update rates";
        $this->asExtension('FormController')->setConfig('bulk_config.yaml');
        $this->asExtension('FormController')->update($recordId, $context);
    }
    
    public function updatebulk_onSave($recordId = null, $context = null) {
        $this->asExtension('FormController')->setConfig('bulk_config.yaml');
        $this->asExtension('FormController')->update_onSave($recordId, $context);
        // or custom logic (if you add custom logic please remove above lines)
    }
    

    现在您可以导航到http://localhost/backend/author/plugin/controller_name/updatebulk/1,它将根据新的bulk_config.yaml 配置文件呈现表单。

    当你保存它时,它会调用updatebulk_onSave来更新记录。我们必须遵守规则,让FormController 处理所有控制以使其正常工作。

    如果您需要以不同的方式保存记录,则需要在 updatebulk_onSave 方法中添加 custom logic,由您自己决定。

    @注意

    如果您还需要creation functionality,则需要其他方法。例如。

    public function createbulk($context = null)
    {
        $this->pageTitle = "Bulk create rates";
        $this->asExtension('FormController')->setConfig('bulk_config.yaml');
        $this->asExtension('FormController')->create($context);
    }
    
    public function createbulk_onSave($context = null) {
        $this->asExtension('FormController')->setConfig('bulk_config.yaml');
        $this->asExtension('FormController')->create_onSave($context);
    }
    

    如有任何疑问,请发表评论。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-06-05
      • 2017-08-28
      • 2019-09-06
      • 1970-01-01
      • 1970-01-01
      • 2019-03-17
      • 1970-01-01
      • 2016-02-27
      相关资源
      最近更新 更多