Q . Controller::__construct() must be an instance of Drupal\\Core\\Form\\FormBuilder, none given

Mike Tyson. asked, Dec 17 ' 2018

Hi I am getting below error while calling my custom form on controller.

The website encountered an unexpected error. Please try again later.
Recoverable fatal error: Argument 1 passed to Drupal\example_module\Controller\ExampleController::__construct() must be an instance of Drupal\Core\Form\FormBuilder, none given, called in D:\xampp\htdocs\htdocs\btcms_git\httpdocs\core\lib\Drupal\Core\Controller\ControllerBase.php on line 116 and defined in Drupal\example_module\Controller\ExampleController->__construct() (line 30 of sites\btcms.local\modules\example_module\src\Controller\ExampleController.php).
Drupal\example_module\Controller\ExampleController->__construct() (Line: 116)
Drupal\Core\Controller\ControllerBase::create(Object) (Line: 28)
Drupal\Core\DependencyInjection\ClassResolver->getInstanceFromDefinition('\Drupal\example_module\Controller\ExampleController') (Line: 123)
Drupal\Core\Controller\ControllerResolver->createController('\Drupal\example_module\Controller\ExampleController::index') (Line: 74)
Drupal\Core\Controller\ControllerResolver->getControllerFromDefinition('\Drupal\example_module\Controller\ExampleController::index', '/admin/example_module') (Line: 91)
Drupal\Core\Controller\ControllerResolver->getController(Object) (Line: 136)
Symfony\Component\HttpKernel\HttpKernel->handleRaw(Object, 1) (Line: 68)
Symfony\Component\HttpKernel\HttpKernel->handle(Object, 1, 1) (Line: 57)
Drupal\Core\StackMiddleware\Session->handle(Object, 1, 1) (Line: 47)
Drupal\Core\StackMiddleware\KernelPreHandle->handle(Object, 1, 1) (Line: 99)
Drupal\page_cache\StackMiddleware\PageCache->pass(Object, 1, 1) (Line: 78)
Drupal\page_cache\StackMiddleware\PageCache->handle(Object, 1, 1) (Line: 47)
Drupal\Core\StackMiddleware\ReverseProxyMiddleware->handle(Object, 1, 1) (Line: 50)
Drupal\Core\StackMiddleware\NegotiationMiddleware->handle(Object, 1, 1) (Line: 23)
Stack\StackedHttpKernel->handle(Object, 1, 1) (Line: 657)
Drupal\Core\DrupalKernel->handle(Object) (Line: 19)

 

  • forms
  • drupal-8
  • controller

1 Answers

use below code in your controller.

<?php

namespace Drupal\\example_module\\Controller;

use Symfony\\Component\\DependencyInjection\\ContainerInterface;
use Drupal\\Core\\Controller\\ControllerBase;
use Drupal\\Core\\Form\\ConfigFormBase;
use Drupal\\Core\\Form\\FormBuilderInterface;
use Drupal\\Core\\Form\\FormBuilder;
use Drupal\\Core\\Form\\FormStateInterface;

/**
 * Controller for akamai purging.
 */
class ExampleController extends ControllerBase {

    /**
     * The form builder.
     *
     * @var \\Drupal\\Core\\Form\\FormBuilder
     */
    protected $formBuilder;

    /**
     * The ExampleController constructor.
     *
     * @param \\Drupal\\Core\\Form\\FormBuilder $formBuilder
     *   The form builder.
     */
    public function __construct(FormBuilder $formBuilder) {
        $this->formBuilder = $formBuilder;
    }

    /**
     * {@inheritdoc}
     *
     * @param \\Symfony\\Component\\DependencyInjection\\ContainerInterface $container
     *   The Drupal service container.
     *
     * @return static
     */
    public static function create(ContainerInterface $container) {
        return new static(
                $container->get(\'form_builder\')
        );
    }

    /**
     * {@inheritdoc}
     */
    public function index() {
	
        $form[\'form\'] = $this->formBuilder->getForm(\'\\Drupal\\example_module\\Form\\MyCustomForm\');

        return $form;
    }

}

 

 

Leave a Ansewer