1

Craft's documentation states: "If the URI matches an entry’s or category’s URI, the section’s/category group’s template will get loaded". Instead of loading the template, how can I set the route to run an action instead? In config/routes.php, I set 'entryurl' => array('action' => 'action/path'), but this does not work.

1 Answer 1

1

You want to take a look at the registerSiteRoutes() method in your plugin class. In this example, it will route a request to your MyPluginHandle_FooController::actionBar() method. foo is obviously the name of the controller, and bar is the method.

public function registerSiteRoutes()
{
    return array(
        'some/route/here' => array('action' => 'myPluginHandle/foo/bar'),
    );
}

Here is what the controller would look like with these naming conventions:

<?php
namespace

class MyPluginHandle_FooController extends BaseController
{
    // Allow anonymous users (users that aren't logged in) to use this controller
    protected $allowAnonymous = true;

    public function actionBar()
    {
        // do something
    }
}

You can also do the same thing for CP routes.

http://buildwithcraft.com/docs/plugins/templates#dynamic-url-routing

2
  • Does action as a suffix actually work or does it need to be a prefix, as in actionBar()? Commented Jan 15, 2015 at 4:48
  • My mistake, you are correct. It's actionBar, I had that inverted on accident. I updated my answer. Commented Jan 15, 2015 at 18:28

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.