I have an issue that i can not use formkey Validator in my custom module, the module is working fine for me if i am just echoing some text messages, but if i construct the $formKeyValidator or the $formKey then will just return the 500 page, and the route url is correct for me

class Index extends \Magento\Framework\App\Action\Action
    protected $formKey;
    protected $formKeyValidator;

    public function __construct(
        \Magento\Framework\App\Action\Context $context,
        \Magento\Framework\Data\Form\FormKey\Validator $formKeyValidator,
        \Magento\Framework\Data\Form\FormKey $formKey
        $this->formKeyValidator  = $formKeyValidator;
        $this->formKey           =  $formKey;
        return parent::__construct($context);

    public function execute()

        echo 'Hello Magento 2! We will change the world!';

enter image description here

Thank you

  • Clear generated code from generated folder at root of magento – Prince Patel Nov 28 '18 at 6:01

First all, we don't need Magento\Framework\Data\Form\FormKey. This class was declared already on Magento\Framework\Data\Form\FormKey\Validator.

Second, try to clear generation code: generated or var/generation folder.


In Magento root index.php file write below code in after PHP tag start.

ini_set('display_errors', 1);

Again reload your page. Now you can show an error of code.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.