Answers.Body: <p><code>craft()</code> is gone in Craft 3; instead you will be accessing <code>Craft::$app-&gt;</code> for most things. I'd suggest generating your Craft 3 plugin scaffolding at <a href="https://pluginfactory.io/" rel="noreferrer">pluginfactory.io</a>, and if you leave code comments on, it'll give you some hints on how to do stuff like this.</p>
  
  <p>This article might also be helpful to you: <a href="https://nystudio107.com/blog/so-you-wanna-make-a-craft-3-plugin" rel="noreferrer">So You Wanna Make a Craft 3 Plugin?</a></p>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2018-02-23T06:23:59.200Z
Answers.Text: In addition to this really good answer I would like to leave this link to the official documentation https://github.com/craftcms/docs/blob/v3/README.md and this link https://github.com/craftcms/docs/blob/v3/en/updating-plugins.md for the most important changes for plugin development
Answers.UserDisplayName: Robin Schambach
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2018-02-23T03:22:09.720Z
Answers.LastActivityDate: 2018-02-23T03:22:09.720Z
Answers.UserDisplayName: andrew.welch
Body: <p>I'm making a Craft 3 plugin to read a CSV file and insert the data as Categories.</p>
  
  <p>I used <a href="https://pluginfactory.io/" rel="nofollow noreferrer">pluginfactory.io</a> to scaffold my plugin.</p>
  
  <p>I can't access <code>craft()</code> in my controller (ImportCsvController.php).</p>
  
  <p><strong>My controller action:</strong></p>
  
  <pre><code>    public function actionDoSomething()
      {
        $categoriesGroups = craft()-&gt;categories-&gt;getAllGroups();
        return json_encode("hi");
      }
  </code></pre>
  
  <p><strong>My namespacing:</strong></p>
  
  <pre><code>&lt;?php
  
  
  namespace workyard\csvtocategoryimporter\controllers;
  use workyard\csvtocategoryimporter\Csvtocategoryimporter;
  use craft\web\Controller;
  use Craft;
  </code></pre>
  
  <p><strong>The error message:</strong></p>
  
  <pre><code>Call to undefined function workyard\csvtocategoryimporter\controllers\craft()
  </code></pre>
  
  <p>Any ideas why I can't access craft(). Do I have to "use" something in the namespace section?</p>
  
Tags: <plugin-development><categories><craft3>
Title: Craft 3 - Can't access the craft() in plugin controller
ThreadText: # Craft 3 - Can't access the craft() in plugin controller
  
  (Asked by: username_0 on 2018-02-23)
  
  I'm making a Craft 3 plugin to read a CSV file and insert the data as Categories.
  
  I used [pluginfactory.io](https://pluginfactory.io/) to scaffold my plugin.
  
  I can't access `craft()` in my controller (ImportCsvController.php).
  
  **My controller action:**
  
  ```
      public function actionDoSomething()
      {
        $categoriesGroups = craft()->categories->getAllGroups();
        return json_encode("hi");
      }
  
  ```
  
  **My namespacing:**
  
  ```
  <?php
  
  namespace workyard\csvtocategoryimporter\controllers;
  use workyard\csvtocategoryimporter\Csvtocategoryimporter;
  use craft\web\Controller;
  use Craft;
  
  ```
  
  **The error message:**
  
  ```
  Call to undefined function workyard\csvtocategoryimporter\controllers\craft()
  
  ```
  
  Any ideas why I can't access craft(). Do I have to "use" something in the namespace section?
  
  **Tags:** plugin-development, categories, craft3
  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_1 on 2018-02-23. Score: 6 | Accepted answer
  
  `craft()` is gone in Craft 3; instead you will be accessing `Craft::$app->` for most things. I'd suggest generating your Craft 3 plugin scaffolding at [pluginfactory.io](https://pluginfactory.io/), and if you leave code comments on, it'll give you some hints on how to do stuff like this.
  
  This article might also be helpful to you: [So You Wanna Make a Craft 3 Plugin?](https://nystudio107.com/blog/so-you-wanna-make-a-craft-3-plugin)
  
  > **Comments:**
  > **username_2**: In addition to this really good answer I would like to leave this link to the official documentation https://github.com/craftcms/docs/blob/v3/README.md and this link https://github.com/craftcms/docs/blob/v3/en/updating-plugins.md for the most important changes for plugin development  
  
  ---
  
  