<?php
namespace my\plugin;
use craft\events\RegisterTemplateRootsEvent;
use craft\events\RegisterUrlRulesEvent;
use craft\web\UrlManager;
use craft\web\View;
use yii\base\Event;
class Plugin extends \craft\base\Plugin
{
public static $plugin;
public $schemaVersion = '1.0.0';
public function init()
{
parent::init();
self::$plugin = $this;
Event::on(UrlManager::class,
UrlManager::EVENT_REGISTER_SITE_URL_RULES,
function (RegisterUrlRulesEvent $event) {
$event->rules['my'] = 'myplugin/top';
}
);
Event::on(UrlManager::class,
UrlManager::EVENT_REGISTER_CP_URL_RULES,
function (RegisterUrlRulesEvent $event) {
$event->rules['my'] = 'myplugin/top/admin';
});
Event::on(View::class,
View::EVENT_REGISTER_SITE_TEMPLATE_ROOTS,
function (RegisterTemplateRootsEvent $event) {
$event->roots['myplugin'] = __DIR__ . '/templates';
});
}
}
<?php
namespace my\plugin\controllers;
use craft\web\Controller;
class TopController extends Controller
{
public function actionIndex() {
return 'my plugin';
}
public function actionHoge()
{
$this->requirePermission('admin');
return 'my plugin admin';
}
}