In the controller documentation for plugins, there's a section that talks about routing to specific actions using a Craft object like this:

var url = Craft.getActionUrl('cocktailRecipes/ingredients/saveIngredient', { id: 10 });

I think this object is the one in /craft/app/resources/js/craft.js, but I can't figure out how to make it available. I tried including it via includeJsResource, but this doesn't appear to be working

{% includeJsResource "resources/js/craft.js" %}

What do I need to include to be able to use this object in my JavaScript (it is not a load ordering issue, the URL generated by includeJsResource goes to the 404 page)?

1 Answer 1


The Craft global variable is available to any Javascript running in the backend/Control Panel, without including anything.

It's not possible to include, reference or use the global for any code running in the frontend.


To provide access to a controller action for frontend Javascript, I'd recommend setting up a site route in your plugin's main class. The following will route the URL http://yourbaseurl/myplugin/foobar/add to ExampleController:

public function registerSiteRoutes()
    return array(
        'myplugin/foobar/add' => array('action' => 'myPlugin/example/add'),

As for the ExampleController, be sure to set the $allowAnonymous class variable to true. This allows visitors that aren't logged in to Craft access (beware that this will make all your controller´s actions accessible without login):

class MyPlugin_ExampleController extends BaseController
   protected $allowAnonymous = true;
   public function actionAdd()
       // Your code here           
  • In that case, how do I reach the business logic from the frontend? I have a plugin with a BasicController that has a method actionAdd, and I've tried navigating to /actions/basicController/add, and I get a 404
    – charmeleon
    Mar 18, 2015 at 23:50
  • The cleanest way would probably be to add a site route and point that to your controller, e.g. 'myplugin/add' => array( 'action' => 'myPlugin/basic/add' ), then call http://yourbaseurl.com/myplugin/add with $.ajax() in your frontend JS. Mar 19, 2015 at 0:04
  • I'm assuming that registerSiteRoutes goes in the BasicPlugin.php file? Because I've added it, and I'm still getting 404s. EDIT: I decided to go into the PluginService and use print_r to see all the Plugins that are loaded, and my plugin, as well as another that I downloaded and dumped into the plugins directory, do not appear in that list
    – charmeleon
    Mar 19, 2015 at 0:17
  • You might need to allow anonymous (i.e. not logged in) access to your controller. This is done by setting the $allowAnonymous variable of your controller class to true: protected $allowAnonymous = true; Mar 19, 2015 at 0:22
  • 1
    Regarding the edit to your comment above – apologize if this is a silly question, but you have installed your plugin, right? Dumping plugins into the plugins directory wont automatically install/activate plugins. Mar 19, 2015 at 0: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.