3

I have created a custom module that for the most part works great, it's frontend routes work flawlessly and it hasn't had issues.

Recently I attempted to create a custom admin grid by following https://www.mageplaza.com/magento-2-module-development/create-admin-grid-magento-2.html#step-1-create-database-schema, but whenever I attempt to access my admin routes I always get redirected to the Sales/Orders page with the message "Invalid security or form key. Please refresh the page".

This error has been asked about probably hundreds of times by this point, but those all seem related to the Magento backend throwing this error, rather than a custom module causing it. Regardless, here are all the solutions I have tried from those posts:

  • Log out/Log in
  • Remove all cookies related to the Magento domain and re log in
  • Make sure cookie_domain and cookie_path are correct in core_config
  • Make sure base URL is 127.0.0.1 instead of localhost(not applicable in my case since I use a dev subdomain)
  • Recompile/setup:upgrade/deploy
  • bin/magento cache:flush/cache:clean
  • Firefox instead of chrome
  • Doublecheck routes

It's worth noting that I have some admin routes that are accessed via the orders page and are mass actions so they don't need to return a page and only need to return to the sales/order page, those actually work and do the function they are intended to but complain about the invalid form key once they redirect to the orders page.

/etc/adminhtml/routes.xml

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:App/etc/routes.xsd">
    <router id="admin">
        <route id="vendor_module" frontName="vendor_module">
            <module name="Vendor_Module"/>
        </route>
    </router>
</config>

/view/adminhtml/layout/vendor_module_store_index.xml

<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../../../../../lib/internal/Magento/Framework/View/Layout/etc/page_configuration.xsd">
    <update handle="styles"/>
    <body>
        <referenceContainer name="content">
            <uiComponent name="vendor_module_store_listing"/>
        </referenceContainer>
    </body>
</page>

/view/adminhtml/ui_component/vendor_module_store_listing.xml

<listing xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Ui:etc/ui_configuration.xsd">
    <argument name="data" xsi:type="array">
        <item name="js_config" xsi:type="array">
            <item name="provider" xsi:type="string">vendor_module_store_listing.vendor_module_store_listing_data_source</item>
            <item name="deps" xsi:type="string">vendor_module_store_listing.vendor_module_store_listing_data_source</item>
        </item>
        <item name="spinner" xsi:type="string">spinner_columns</item>
        <item name="buttons" xsi:type="array">
            <item name="add" xsi:type="array">
                <item name="name" xsi:type="string">add</item>
                <item name="label" xsi:type="string" translate="true">Add New Store</item>
                <item name="class" xsi:type="string">primary</item>
                <item name="url" xsi:type="string">*/*/new</item>
            </item>
        </item>
    </argument>
    <dataSource name="nameOfDataSource">
        <argument name="dataProvider" xsi:type="configurableObject">
            <argument name="class" xsi:type="string">Magento\Framework\View\Element\UiComponent\DataProvider\DataProvider</argument>
            <argument name="name" xsi:type="string">vendor_module_store_listing_data_source</argument>
            <argument name="primaryFieldName" xsi:type="string">post_id</argument>
            <argument name="requestFieldName" xsi:type="string">id</argument>
            <argument name="data" xsi:type="array">
                <item name="config" xsi:type="array">
                    <item name="component" xsi:type="string">Magento_Ui/js/grid/provider</item>
                    <item name="update_url" xsi:type="url" path="mui/index/render"/>
                    <item name="storageConfig" xsi:type="array">
                        <item name="indexField" xsi:type="string">entity_id</item>
                    </item>
                </item>
            </argument>
        </argument>
    </dataSource>
    <columns name="spinner_columns">
        <selectionsColumn name="ids">
            <argument name="data" xsi:type="array">
                <item name="config" xsi:type="array">
                    <item name="resizeEnabled" xsi:type="boolean">false</item>
                    <item name="resizeDefaultWidth" xsi:type="string">55</item>
                    <item name="indexField" xsi:type="string">entity_id</item>
                </item>
            </argument>
        </selectionsColumn>
        <column name="entity_id">
            <argument name="data" xsi:type="array">
                <item name="config" xsi:type="array">
                    <item name="filter" xsi:type="string">textRange</item>
                    <item name="sorting" xsi:type="string">asc</item>
                    <item name="label" xsi:type="string" translate="true">ID</item>
                </item>
            </argument>
        </column>
        <column name="title">
            <argument name="data" xsi:type="array">
                <item name="config" xsi:type="array">
                    <item name="filter" xsi:type="string">text</item>
                    <item name="editor" xsi:type="array">
                        <item name="editorType" xsi:type="string">text</item>
                        <item name="validation" xsi:type="array">
                            <item name="required-entry" xsi:type="boolean">true</item>
                        </item>
                    </item>
                    <item name="label" xsi:type="string" translate="true">title</item>
                </item>
            </argument>
        </column>
        <column name="demo_start" class="Magento\Ui\Component\Listing\Columns\Date">
            <argument name="data" xsi:type="array">
                <item name="config" xsi:type="array">
                    <item name="filter" xsi:type="string">dateRange</item>
                    <item name="component" xsi:type="string">Magento_Ui/js/grid/columns/date</item>
                    <item name="dataType" xsi:type="string">date</item>
                    <item name="label" xsi:type="string" translate="true">tart Date</item>
                </item>
            </argument>
        </column>
        <column name="created_at" class="Magento\Ui\Component\Listing\Columns\Date">
            <argument name="data" xsi:type="array">
                <item name="config" xsi:type="array">
                    <item name="filter" xsi:type="string">dateRange</item>
                    <item name="component" xsi:type="string">Magento_Ui/js/grid/columns/date</item>
                    <item name="dataType" xsi:type="string">date</item>
                    <item name="label" xsi:type="string" translate="true">Created</item>
                </item>
            </argument>
        </column>
        <column name="updated_at" class="Magento\Ui\Component\Listing\Columns\Date">
            <argument name="data" xsi:type="array">
                <item name="config" xsi:type="array">
                    <item name="filter" xsi:type="string">dateRange</item>
                    <item name="component" xsi:type="string">Magento_Ui/js/grid/columns/date</item>
                    <item name="dataType" xsi:type="string">date</item>
                    <item name="label" xsi:type="string" translate="true">Modified</item>
                </item>
            </argument>
        </column>
    </columns>
</listing>

At the risk of cluttering up this question I have omitted the other usual files that I'm pretty sure aren't related but if there are any other files you would like to see to help me debug, I can add them.

1 Answer 1

7

In this case, I was accessing the wrong route, rather than /adminurl/vendor_module/store/ which caused the error message about an invalid key, I needed to go to /adminurl/vendor_module/store/index/.

I still think Magento should return a 404 or some other error message that would be much more helpful to debug but in this case that was all I did wrong.

2
  • 2
    In the hopes that I can help someone desperately searching and the above doesn't work: if you are building an adminhtml page ensure you put your controller inside of Vendor/Module/Controller/**Adminhtml**/.... This simple mistake cost me a lot of time Commented Jan 6, 2021 at 14:22
  • Just in case it helps anyone: the issue in my case is I added the layout but not the controller.
    – bfontaine
    Commented Apr 19, 2021 at 13:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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