1

I have a plugin that has a menu and in backend which basically has a button that calls the plugins default controller actionXXXX method.

The action does some database / api work then is meant to return a result in a results template.

I have no idea how plugins got so complicated in craft and Im about ready to give up after today. The plugin main file supplies EVENT_REGISTER_CP_URL_RULES

 Event::on(
            UrlManager::class,
            UrlManager::EVENT_REGISTER_CP_URL_RULES,
            function (RegisterUrlRulesEvent $event) {
                $event->rules['mapit-api2entry/default/map-it'] = 'results';
            }
        );

The plugin form in the cp has a hidden action like so. Do I need admin here? I assume so because we are in the CP.

<input type="hidden" name="action" value="admin/actions/mapit-api2entry/default/map-it">

I can put the url "admin/actions/mapit-api2entry/default/map-it" in the browser and pull up my template as expected.

But when I hit the submit button I get:

Unable to resolve the request "admin/actions/mapit-api2entry/default/map-it".

How do I tell the plugin / cp the right route to use after the action is triggered? Is this case of camel -vs kebab somewhere?

1 Answer 1

3

I think my other answer is also part of this one.

The only specifics I'd add here is that your action is getting "double-declared," in a way. Using the action param in a request has a lot of weight in Craft's eye—but you're stacking two strategies, and the system can't match it with your intent.

My recommendation is (whenever possible) to use Craft's Twig helpers:

<form method="POST">
    {{ csrfInput() }}
    {{ actionInput('mapit-api2entry/default/map-it') }}

    <button>Go!</button>
</form>

This will generate a hidden action input that looks like this:

<input name="action" value="mapit-api2entry/default/map-it">

Notice that we don't declare action as part of the route! Craft already knows you want to route directly to a Controller/Action path because it sees the action param.

The direct-access behavior you see is a vanity/convenience feature—the actionTrigger (the first segment in that path, aside from the cpTriggeradmin by default) is actually customizable, and not particularly useful in my mind, when the Twig helpers (and action query/POST param) are so reliable.

2
  • Thanks for taking the time to reply.(lovely work by the way ) Finally got a result. So is there no need to tell craft that this is a cptrigger - is this done in the route? $event->rules['mapit-api2entry/default/map-it'] = 'results'; And or in the controller? Eg $this->renderTemplate('xxx')
    – joomkit
    Commented Sep 15, 2021 at 15:15
  • 1
    Keep in mind that the "key" you assign to the rules array is the alias to your controller action, and the "value" (what is assigned) is the full controller path: $event->rules['my-shortcut-route'] = 'mapit-api2entry/default/map-it';, where DefaultController::actionMapIt() becomes available at my-shortcut-route (or admin/my-shortcut-route, if you're in the CP). I'm not entirely sure how the determinations of an "Action Request" and a "CP Request" overlap—but generally, actions are for logic + processing, and they redirect to an admin/my-route in success/fail states. Commented Sep 16, 2021 at 16:46

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.