1

Is it possible to use a custom route for your plugin's CP section, instead of cpTrigger/plugin-handle, or to direct cpTrigger/plugin-handle to a different template other than plugin-handle/index.twig?

1

From your Plugin class, add this code:

use craft\events\RegisterUrlRulesEvent;
use craft\web\UrlManager;
use yii\base\Event;

// ...

class Plugin extends \craft\base\Plugin
{
    $hasCpSection = true;

    public function init()
    {
        // Register a custom CP route
        Event::on(UrlManager::class, UrlManager::EVENT_REGISTER_CP_URL_RULES, function(RegisterUrlRulesEvent $e) {
            $e->rules['your/custom/uri'] = ['template' => 'plugin-handle/index'];
        });
    }

    public function getCpNavItem()
    {
        // Use the default name & icon, but customize the URL
        $navItem = parent::getCpNavItem();
        $navItem['url'] = 'your/custom/uri';
        return $navItem;
    }

    // ...
}
| improve this answer | |
1

Of course you can Link

Event::on(UrlManager::class, UrlManager::EVENT_REGISTER_CP_URL_RULES, function(RegisterUrlRulesEvent $event) {
    $event->rules['whateveryoulike'] = ['template' => 'cocktails/_edit'];
    $event->rules['cocktails/<widgetId:\d+>'] = 'cocktails/edit-cocktail';
});

So the url www.example.com/admin/whateveryoulike redirects you to the template cocktails/_edit. You don't need to use the plugin-handle, you can overwrite everything but you have to make sure no other plugin overwrites your routes..

| improve this answer | |

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.