Use custom form submit function inside class In my controller I am loading a form and including one custom submit handler. However the submit function is not called. I think that it's either not found or not accessible as it is inside the controller class:

class MyController extends ControllerBase {

  //...

  public function myProfile() {
    $user = User::load($this-&gt;currentUser-&gt;id());
    $form = $this-&gt;entityFormBuilder()-&gt;getForm($user);

    $form['actions']['submit']['#submit'][] = '::_custom_submit_form';
  }

  protected function _custom_submit_form(array &amp;$form, \Drupal\Core\Form\FormStateInterface $form_state) { 
    $form_state-&gt;setRedirect('&lt;front&gt;');
  }

}


If I use hook_form_alter it works, but I don't want to use a hook as I can directly manipulate the form.
To understand why your code is not working, you need to understand how the form API works:


The form definition is called
Any form alter hooks are executed, altering the form
The render array is cached
The form is rendered as HTML and sent to the browser
The user submits the form
Drupal checks that the submitted values match the cached form. If values do not match, the values are rejected by Drupal's security system


When you call this code:

$form = $this-&gt;entityFormBuilder()-&gt;getForm($user);


The caching mentioned in step 3 above has already happened. So when you do this after that line of code:

$form['actions']['submit']['#submit'][] = '::_custom_submit_form';


The submit handler is never called, since the form was cached before this line of code, and therefore your submit handler does not make it into the cache.

To add a submit handler to a form, you need to implement hook_form_alter() (or a variation), which will add the handler before the caching happens. Or, you can extend the form class you are working with, add your own ::submitForm() handler, then call the parent submit from that.