0

My situation:

I created a new token_table (with InstallSchema.php) to store the token that will be needed when sending an API request to my endpoint.

Now I managed to create a form where admin user can enter the token and save it.

But after clicking on the "Save" button I get the following:

enter image description here

Can anyone suggest what the problem might be?

app\code\Dev\RestApi\Block\Adminhtml\Token\Edit\Form.php

<?php

namespace Dev\RestApi\Block\Adminhtml\Token\Edit;

use Magento\Backend\Block\Widget\Form\Generic;

class Form extends Generic
{
    protected function _prepareForm()
    {
        $form = $this->_formFactory->create(
            [
                'data' => [
                    'id' => 'edit_form',
                    'action' => $this->getUrl('*/*/save'),
                    'method' => 'post'
                ],
            ]
        );

        $fieldset = $form->addFieldset(
            'base_fieldset',
            ['legend' => __('Token Information')]
        );

        $fieldset->addField(
            'token',
            'text',
            [
                'name' => 'token',
                'label' => __('Token'),
                'title' => __('Token'),
                'required' => true,
            ]
        );

        $form->setUseContainer(true);
        $this->setForm($form);

        return parent::_prepareForm();
    }
}

app\code\Dev\RestApi\Controller\Adminhtml\Token\Save.php

<?php


namespace Dev\RestApi\Controller\Adminhtml\Token;

use Magento\Backend\App\Action;
use Dev\RestApi\Model\TokenFactory;

class Save extends Action
{
    protected $tokenFactory;

    public function __construct(
        Action\Context $context,
        TokenFactory $tokenFactory
    ) {
        parent::__construct($context);
        $this->tokenFactory = $tokenFactory;
    }

    public function execute()
    {
        $data = $this->getRequest()->getPostValue();

        if ($data) {
            try {
                $tokenModel = $this->tokenFactory->create();
                $tokenModel->setData($data);
                $tokenModel->save();

                $this->messageManager->addSuccessMessage(__('Token has been saved.'));
                $this->_redirect('*/*/index');
                return;
            } catch (\Exception $e) {
                $this->messageManager->addErrorMessage($e->getMessage());
            }
        }

        $this->_redirect('*/*/index');
    }
}

app\code\Dev\RestApi\view\adminhtml\layout\dev_controller_index.xml

<?xml version="1.0"?>
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
    <referenceBlock name="page.title">
        <action method="setPageTitle">
            <argument name="title" xsi:type="string">Module</argument>
        </action>
    </referenceBlock>
    <body>
        <referenceContainer name="content">
            <block class="Dev\RestApi\Block\Adminhtml\Token\Edit" name="token_edit" />
        </referenceContainer>
    </body>
</page>

1 Answer 1

0

Did you register your route, it looks like you didn't create adminhtml route like:

<?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="mageplaza_helloworld" frontName="mageplaza_helloworld">
            <module name="Dev_RestApi"/>
        </route>
    </router>
</config>

that's why its returning the 404 Url. Please follow the full instruction here: https://www.mageplaza.com/devdocs/create-admin-grid-magento-2.html

4
  • Hello! Thanks for your reply! I just downloaded this example and I see the same problem in this module. I tried deleting a record from the database and got exactly the same problem. Also, based on your recommendation on routes.xml I have everything the same, the only difference is the line -> <route id="dev_restapi" frontName="dev_restapi">
    – User
    Commented Sep 13, 2023 at 14:10
  • So is it work for you? or is it still running 404 URL? Commented Sep 13, 2023 at 16:15
  • Unfortunately I still see the 404 error code
    – User
    Commented Sep 14, 2023 at 9:45
  • Can you send me your whole module code? So I can try and. fix this Commented Sep 14, 2023 at 10:59

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.