I love routes, but they appear limited to loading a specific template. What about loading a plugin/controller based on the route instead? Anyway to do this in craft or am I limited to .htaccess?


This is indeed possible. You'll want to update your route to point to an array with and identify your controller path with the key action:

public function registerCpRoutes()
  return array(
    'pluginname/edit' => array('action' => 'pluginName/editItem'),

In your plugin controller, you can create any number of variables you need and hand them off to your template as the second argument in the renderTemplate() method:

// PluginNameController.php
public function actionEditItem()
    // Create any variables you want available in your template
    $variables['variable'] = craft()->request->getSegment(3);       

    $variables['items'] = craft()->pluginName->getAllItems();

    // Load a particular template and with all of the variables you've created
    $this->renderTemplate('pluginname/_edit', $variables);

Check out the article How do I setup a route to point to a controller action instead of a template? on Straight Up Craft for a more in depth discussion that also goes into how to render the template from your control, hand off variables to your template, and reference multiple controllers.

  • Ben, this is just what I was looking for. How would I pass in a variable using CpRoutes(). ie. 'pluginname/edit/variable' => array('action' => 'pluginName/editItem'), Sep 30 '14 at 13:39
  • @DavidAaronMcInnis I've updated the answer to give an example of how to create variables for your template in the controller. Sep 30 '14 at 15:31

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.