October CMS resources and help articles

Simple and to the point. Optimized by the community.

Use an AJAX request handler on a backend settings form

8
by OFFLINE, last modified on May 17th, 2020

A backend settings form usually only has a form model, but no separate controller available. If you want to register a custom AJAX request handler you have to add it to the \System\Controllers\Settings controller. October re-uses this controller for all backend settings pages.

You can use the extend() method to add a custom handler:

    // Plugin.php
    public function boot()
    {
        \System\Controllers\Settings::extend(function($controller) {
            $controller->addDynamicMethod('onMyCustomHandler', function() {
                return 'handler called!';
            });
        });
    }

You can now call this handler from any custom partial in your backend settings form:

<button class="btn btn-default"
        data-handler="onMyCustomHandler">
    Trigger AJAX request
</button>

Discussion

0 comments

We use cookies to measure the performance of this website. Do you want to accept these cookies?