Answers.Body: <p>The <code>Craft</code> global variable is available to any Javascript running in the backend/Control Panel, without including anything.</p>
  
  <p>It's not possible to include, reference or use the global for any code running in the frontend.</p>
  
  <hr>
  
  <p>EDIT:</p>
  
  <p>To provide access to a controller action for frontend Javascript, I'd recommend setting up a <a href="http://buildwithcraft.com/docs/plugins/hooks-reference#registerSiteRoutes" rel="nofollow">site route</a> in your plugin's main class. The following will route the URL <code>http://yourbaseurl/myplugin/foobar/add</code> to <code>ExampleController</code>:</p>
  
  <pre><code>public function registerSiteRoutes()
  {
      return array(
          'myplugin/foobar/add' =&gt; array('action' =&gt; 'myPlugin/example/add'),
      );
  }
  </code></pre>
  
  <p>As for the ExampleController, be sure to set the <code>$allowAnonymous</code> class variable to <code>true</code>. This allows visitors that aren't logged in to Craft access (beware that this will make <em>all</em> your controller´s actions accessible without login):</p>
  
  <pre><code>class MyPlugin_ExampleController extends BaseController
  {
     protected $allowAnonymous = true;
     public function actionAdd()
     {
         // Your code here           
     }
  }
  </code></pre>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-03-18T23:50:47.083Z
Answers.Text: 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
Answers.UserDisplayName: charmeleon
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-03-19T00:04:19.873Z
Answers.Text: The cleanest way would probably be to add a [site route](http://buildwithcraft.com/docs/plugins/hooks-reference#registerSiteRoutes) 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.
Answers.UserDisplayName: Mats Mikkel Rummelhoff
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-03-19T00:17:38.133Z
Answers.Text: 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
Answers.UserDisplayName: charmeleon
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-03-19T00:22:24.607Z
Answers.Text: 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;```
Answers.UserDisplayName: Mats Mikkel Rummelhoff
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-03-19T00:23:19.637Z
Answers.Text: I can confirm that `$allowAnonymous` is protected, true, and not misspelled
Answers.UserDisplayName: charmeleon
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-03-19T00:25:21.023Z
Answers.Text: Well, then you might need to post your base plugin class and controller. Difficult to say where this goes wrong for you.
Answers.UserDisplayName: Mats Mikkel Rummelhoff
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-03-19T00:28:34.197Z
Answers.Text: 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.
Answers.UserDisplayName: Mats Mikkel Rummelhoff
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-03-19T00:31:50.143Z
Answers.Text: Um, I have not. I just started reading the Plugin docs and coding away in the plugins directory. Craft was already setup so I was unaware that there was an installation step for the plugin. I have admin credentials though, where do I install the plugin from?
Answers.UserDisplayName: charmeleon
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-03-19T00:36:09.990Z
Answers.Text: Visit the Settings area in the CP by clicking the upper right cogwheel icon – you'll find a link to the plugin management area from there.
Answers.UserDisplayName: Mats Mikkel Rummelhoff
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-03-19T00:44:38.470Z
Answers.Text: Excellent! It works. You're a saint, thanks x 1000, I mean it
Answers.UserDisplayName: charmeleon
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-03-19T00:45:23.153Z
Answers.Text: Awesome – my pleasure!
Answers.UserDisplayName: Mats Mikkel Rummelhoff
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-03-19T00:58:22.167Z
Answers.Text: _Edited the answer to provide info on how to set up a site route pointing to a controller action._
Answers.UserDisplayName: Mats Mikkel Rummelhoff
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-03-19T01:09:05.783Z
Answers.Text: You're a machine, @MatsMikkelRummelhoff.
Answers.UserDisplayName: Brad Bell
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-03-19T01:15:13.960Z
Answers.Text: I wish, @BradBell :)
Answers.UserDisplayName: Mats Mikkel Rummelhoff
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-03-18T21:23:42.437Z
Answers.LastActivityDate: 2015-03-19T00:57:23.603Z
Answers.LastEditDate: 2015-03-19T00:57:23.603Z
Answers.UserDisplayName: Mats Mikkel Rummelhoff
Body: <p>In the <a href="http://buildwithcraft.com/docs/plugins/controllers" rel="nofollow">controller documentation</a> for plugins, there's a section that talks about routing to specific actions using a <code>Craft</code> object like this:</p>
  
  <pre><code>var url = Craft.getActionUrl('cocktailRecipes/ingredients/saveIngredient', { id: 10 });
  </code></pre>
  
  <p>I think this object is the one in <code>/craft/app/resources/js/craft.js</code>, but I can't figure out how to make it available. I tried including it via <code>includeJsResource</code>, but this doesn't appear to be working</p>
  
  <pre><code>{% includeJsResource "resources/js/craft.js" %}
  </code></pre>
  
  <p>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)?</p>
  
Tags: <plugin-development><javascript>
Title: Include Craft JS object
ThreadText: # Include Craft JS object
  
  (Asked by: username_0 on 2015-03-18)
  
  In the [controller documentation](http://buildwithcraft.com/docs/plugins/controllers) 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)?
  
  **Tags:** plugin-development, javascript
  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_1 on 2015-03-18. Score: 2 | Accepted answer
  
  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.
  
  ---
  
  EDIT:
  
  To provide access to a controller action for frontend Javascript, I'd recommend setting up a [site route](http://buildwithcraft.com/docs/plugins/hooks-reference#registerSiteRoutes) 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           
     }
  }
  
  ```
  
  > **Comments:**
  > **username_0**: 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  
  > **username_1**: The cleanest way would probably be to add a [site route](http://buildwithcraft.com/docs/plugins/hooks-reference#registerSiteRoutes) 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.  
  > **username_0**: 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  
  > **username_1**: 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;```  
  > **username_0**: I can confirm that `$allowAnonymous` is protected, true, and not misspelled  
  > **username_1**: Well, then you might need to post your base plugin class and controller. Difficult to say where this goes wrong for you.  
  > **username_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.  
  > **username_0**: Um, I have not. I just started reading the Plugin docs and coding away in the plugins directory. Craft was already setup so I was unaware that there was an installation step for the plugin. I have admin credentials though, where do I install the plugin from?  
  > **username_1**: Visit the Settings area in the CP by clicking the upper right cogwheel icon – you'll find a link to the plugin management area from there.  
  > **username_0**: Excellent! It works. You're a saint, thanks x 1000, I mean it  
  > **username_1**: Awesome – my pleasure!  
  > **username_1**: _Edited the answer to provide info on how to set up a site route pointing to a controller action._  
  > **username_2**: You're a machine, @username_1.  
  > **username_1**: I wish, @username_2 :)  
  
  ---
  
  