1

So here is what I see in a controller:

 $event->rules['cpActionTrigger1'] = 'plugin/default/do-something';

What does the first part 'cpActionTrigger1' refer to and what does the second part refer to. I am guessing route and template but never see it spelled out in docs.

1 Answer 1

2

This looks like it’d be in a closure registered on the UrlManager::EVENT_REGISTER_CP_URL_RULES event, like this:

Event::on(
    UrlManager::class,
    UrlManager::EVENT_REGISTER_CP_URL_RULES,
    function (RegisterUrlRulesEvent $e) {
        $e->rules['my/route'] => 'module/controller/action';
    });

The above explains it almost in context—when creating a route with a plain string, it's interpreted as:

<module-id>/<controller-name>/<action-name>

The "Module ID" is either defined by the Plugin in question, or when you declare modules in app.php:

<?php

return [
    '*' => [
        // Lots of other irrelevant (but important) stuff redacted, here!
        'modules' => [
            'bizniz' => \modules\mymodule\BusinessStuff::class,
        ],
     ],
];

If you had a Controller in your Module (and have properly declared BusinessStuff::$controllerNamespace) that looked like this…

<?php

namespace modules\mymodule\controllers;

use craft\base\Controller;

class FinanceController extends Controller
{
    public function actionCountBeans()
    {
        return $this->asJson(['profit' => (rand() / getrandmax()) * 1000000]);
    }
}

…your "action path" would be bizniz/finance/count-beans. Yii takes care of mapping the path to your controller—and the UrlManager ensures the key you assign it to acts as an alias.

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.