3

Before I get tagged as a duplicate I have already tried everything in the 'duplicate' issues on here to no avail so I'm posting my exact config to see what I'm doing wrong.

Here is my plugin code and my controller code

// My plugin and init funciton
use belniakmedia\sitedominjector\controllers\DefaultController;
class SiteDomInjector extends Plugin
{
    /**
     * @var SiteDomInjector
     */
    public static $plugin;

    /** @var array */
    public $controllerMap = [
        'default' => DefaultController::class,
    ];
    public function init()
    {
        parent::init();
        self::$plugin = $this;

        // Register our site routes
        Event::on(
            UrlManager::class,
            UrlManager::EVENT_REGISTER_SITE_URL_RULES,
            function (RegisterUrlRulesEvent $event) {
                $event->rules['echo'] = 'site-dom-injector/default/index';
            }
        );
    }
}

// My Controller
class DefaultController extends Controller
{
    protected $allowAnonymous = true;

    public function actionIndex()
    {
        $result = 'Welcome to the DefaultController actionIndex() method';
        return $result;
    }
}

I really don't know if that controller map is necessary or not but one of the posts on here said they got it to work setting that up, so thats what I did.

Here is a photo of my plugin's file structure for continuity:

enter image description here I really have no idea why this isnt working but no matter what I do, when I try to browse to http://site.local/echo I get a 404 not found and the trace seems like its not even attempting to load the controller and is just looking for a matching template file... :(

Any ideas?

  • Before anyone asks, yes the plugin is installed and enabled! – SublymeRick Nov 16 '17 at 23:37
  • I'm not sure if that's really the problem but have you tried to route to site-dom-injector/default when I create your plugin with Pluginfactory.io the default output is $event->rules['siteActionTrigger1'] = 'site-dom-injector/default-controller'; so - I could be totally wrong - but maybe the controller will only route to Index if you don't declare anything – Robin Schambach Nov 17 '17 at 6:49
3

The problem ended up being something underlying in craft or yii2 that will not route the word "echo".

I changed out "echo" to "test" and it works fine. Even with both routes in, one does not and one does:

    // Register our site routes
    Event::on(
        UrlManager::class,
        UrlManager::EVENT_REGISTER_SITE_URL_RULES,
        function (RegisterUrlRulesEvent $event) {
            $event->rules['echo'] = 'site-dom-injector/default/index';
            $event->rules['test'] = 'site-dom-injector/default/index';
        }
    );
  • 1
    That's... weird. – Brad Bell Nov 17 '17 at 18:57
  • huh. I guess because it's a reserved word in PHP? But even that doesn't make sense, I can't see how it'd care from a routing perspective. Is it possible that there's a server-side setting that's causing that request to just never hit Craft? – andrew.welch Nov 19 '17 at 19:17

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.