Answers.Body: <p>To simply pass the variables to your plugin or module use:</p>
  
  <pre><code>MyPlugin::getInstance()-&gt;checkCommerceRecord($user, $dateTime);
  </code></pre>
  
  <p>To pass to a service (see the <a href="https://docs.craftcms.com/v3/extend/services.html#creating-a-service" rel="nofollow noreferrer">Creating a Service</a> section of the docs) use:</p>
  
  <pre><code>MyPlugin::getInstance()-&gt;myService-&gt;checkCommerceRecord($user, $dateTime);
  </code></pre>
  
  <p>To pass it with your model use:</p>
  
  <pre><code>$model = new WFWPluginModel(['setUser' =&gt; [$userId, $dateTime]]);
  
  MyPlugin::getInstance()-&gt;myService-&gt;checkCommerceRecord($model);
  </code></pre>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2018-12-06T12:13:30.413Z
Answers.Text: Ah ok, that means that i would create a service that saves the post data as variables from my controller and i can then access the variables in e.g. my main plugin class
Answers.UserDisplayName: HendrikEng
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2018-12-06T12:19:16.950Z
Answers.Text: You don't have to "save" them per se, you can simply pass them as parameters. I've added an example to my answer for doing it wiith a module.
Answers.UserDisplayName: Ben Croker
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2018-12-06T11:56:29.457Z
Answers.LastActivityDate: 2018-12-06T12:19:14.123Z
Answers.LastEditDate: 2018-12-06T12:19:14.123Z
Answers.UserDisplayName: Ben Croker
Body: <p>I am using a Controller to get Variables from a field in my Plugin.
  How and what would be the best way to pass and use them in other <code>classes</code> or my main plugin file.</p>
  
  <p>I thought about using <code>Sessions</code> or passing them via URL Parameters, but i am not sure if thats the most convenient way.</p>
  
  <p>Thats my controller:</p>
  
  <pre><code>use craft\web\Controller;
  
  class SetUserController extends Controller {
      public function actionSubmitUser() {
  
          $request = \Craft::$app-&gt;getRequest();
  
          $dateTime = $request-&gt;getParam('dateTime');
          $user = $request-&gt;getRequiredParam('user');
  
      }
  }  
  </code></pre>
  
  <p>e.g. in my main plugin file i would like to pass the variable to a function:</p>
  
  <pre><code>// check if commerce record for craft user exists
  public function checkCommerceRecord($user) {
      return $this-&gt;customers-&gt;getCustomerByUserId($user);
  }
  </code></pre>
  
  <p><strong>Update:</strong></p>
  
  <p>I figured that i need to setup a module, however i cant save the variables into the module, getting <code>Setting unknown property: wewereyoung\wfwplugin\models\WFWPluginModel::userId</code></p>
  
  <p>Module:
  
  
  <pre><code>namespace wewereyoung\wfwplugin\models;
  
  use craft\base\Model;
  use craft\validators\ArrayValidator;
  
  class WFWPluginModel extends Model {
  
      public $setUser = [
          'userId',
          'timeDate'
      ];
  
      public function rules() {
          return [
              [
                  [
                      'setUser'
                  ],
                  ArrayValidator::class
              ]
          ];
      }
  }
  </code></pre>
  
  <p>Updated Controller:</p>
  
  <p>
  
  <pre><code>namespace wewereyoung\wfwplugin\controllers;
  
  use craft\web\Controller;
  use wewereyoung\wfwplugin\models\WFWPluginModel;
  
  class SetUserController extends Controller {
      public function actionSubmitUser() {
  
          $request = \Craft::$app-&gt;getRequest();
  
          $dateTime = $request-&gt;getParam( 'dateTime' );
          $userId   = $request-&gt;getParam( 'user' );
  
          $setUser            = new WFWPluginModel();
          $setUser-&gt;userId   = $userId;
          $setUser-&gt;dateTime = $dateTime;
  
      }
  }
  </code></pre>
  
Tags: <plugin-development><craft3>
Title: Pass Controller Variables into a Model
ThreadText: # Pass Controller Variables into a Model
  
  (Asked by: username_0 on 2018-12-04)
  
  I am using a Controller to get Variables from a field in my Plugin.
  How and what would be the best way to pass and use them in other `classes` or my main plugin file.
  
  I thought about using `Sessions` or passing them via URL Parameters, but i am not sure if thats the most convenient way.
  
  Thats my controller:
  
  ```
  use craft\web\Controller;
  
  class SetUserController extends Controller {
      public function actionSubmitUser() {
  
          $request = \Craft::$app->getRequest();
  
          $dateTime = $request->getParam('dateTime');
          $user = $request->getRequiredParam('user');
  
      }
  }  
  
  ```
  
  e.g. in my main plugin file i would like to pass the variable to a function:
  
  ```
  // check if commerce record for craft user exists
  public function checkCommerceRecord($user) {
      return $this->customers->getCustomerByUserId($user);
  }
  
  ```
  
  **Update:**
  
  I figured that i need to setup a module, however i cant save the variables into the module, getting `Setting unknown property: wewereyoung\wfwplugin\models\WFWPluginModel::userId`
  
  Module:
  
  ```
  namespace wewereyoung\wfwplugin\models;
  
  use craft\base\Model;
  use craft\validators\ArrayValidator;
  
  class WFWPluginModel extends Model {
  
      public $setUser = [
          'userId',
          'timeDate'
      ];
  
      public function rules() {
          return [
              [
                  [
                      'setUser'
                  ],
                  ArrayValidator::class
              ]
          ];
      }
  }
  
  ```
  
  Updated Controller:
  
  ```
  namespace wewereyoung\wfwplugin\controllers;
  
  use craft\web\Controller;
  use wewereyoung\wfwplugin\models\WFWPluginModel;
  
  class SetUserController extends Controller {
      public function actionSubmitUser() {
  
          $request = \Craft::$app->getRequest();
  
          $dateTime = $request->getParam( 'dateTime' );
          $userId   = $request->getParam( 'user' );
  
          $setUser            = new WFWPluginModel();
          $setUser->userId   = $userId;
          $setUser->dateTime = $dateTime;
  
      }
  }
  
  ```
  
  **Tags:** plugin-development, craft3
  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_1 on 2018-12-06. Score: 2 | Accepted answer
  
  To simply pass the variables to your plugin or module use:
  
  ```
  MyPlugin::getInstance()->checkCommerceRecord($user, $dateTime);
  
  ```
  
  To pass to a service (see the [Creating a Service](https://docs.craftcms.com/v3/extend/services.html#creating-a-service) section of the docs) use:
  
  ```
  MyPlugin::getInstance()->myService->checkCommerceRecord($user, $dateTime);
  
  ```
  
  To pass it with your model use:
  
  ```
  $model = new WFWPluginModel(['setUser' => [$userId, $dateTime]]);
  
  MyPlugin::getInstance()->myService->checkCommerceRecord($model);
  
  ```
  
  > **Comments:**
  > **username_0**: Ah ok, that means that i would create a service that saves the post data as variables from my controller and i can then access the variables in e.g. my main plugin class  
  > **username_1**: You don't have to "save" them per se, you can simply pass them as parameters. I've added an example to my answer for doing it wiith a module.  
  
  ---
  
  