3

I'm building a custom plugin and need to have a publicly accessible (non-CP) route as part of the plugin.

From what I've read I can use registerSiteRoutes() to register non-CP routes within my plugin. However, I'm getting a 404 when I do this. Here's my setup.

MyPlugin.php

<?php
namespace Craft;

  class MyPlugin extends BasePlugin {

    ...

    public function registerSiteRoutes() {
      return array(
        'myroute'           => array('action' => 'myplugin/access/index')
      );
    }
  }

MyPlugin_AccessController.php

<?php
namespace Craft;

class Portal_AccessController extends BaseController {

  protected $allowAnonymous = true;

  public function index() {

    $templatesPath = craft()->path->pluginsPath().'myplugin/templates/site/';
    craft()->path->setTemplatesPath($templatesPath);

    $this->renderTemplate('index');

  }

}

I have setup a basic template at /plugins/myplugin/templates/site/index.html

Hitting mydomain.com/myplugin results in a 404. Am I thinking of this correctly? Is it possible to have a non-CP route that points to a controller method in my plugin?

6

There are a couple of issues here:

  1. Any action methods in the controller class needs the "action" prefix to their name, i.e. actionIndex() for your "index" action, or actionLogin() for another action route calling "myPlugin/access/login".

In other words, rename your index() method in the controller to actionIndex(), and it should work.

  1. For action routes, plugin handles should be written in camelcase – i.e. "myPlugin/access/index", not "myplugin/access/index".

  2. You have a minor typo in the controller:

    $templatesPath = craft()->path->pluginsPath().'myplugin/templates/site/';

should read

$templatesPath = craft()->path->getPluginsPath().'myplugin/templates/site/';

Addendum: this might just be a typo in the question text, but your controller class is actually named "Portal_AccessController", so it will obviously not respond to the route "myPlugin/access/index" – the latter should read "portal/access/index" (or the controller should be renamed "MyPlugin_AccessController)

  • Not sure why this thing not working for me and keeps giving Unable to resolve the request – Volatil3 Oct 20 '15 at 16:23

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.