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);


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


namespace wewereyoung\wfwplugin\models;

use craft\base\Model;
use craft\validators\ArrayValidator;

class WFWPluginModel extends Model {

    public $setUser = [

    public function rules() {
        return [

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;


1 Answer 1


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 section of the docs) use:

MyPlugin::getInstance()->myService->checkCommerceRecord($user, $dateTime);

To pass it with your model use:

$model = new WFWPluginModel(['setUser' => [$userId, $dateTime]]);

  • 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
    – HendrikEng
    Dec 6, 2018 at 12:13
  • 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.
    – Ben Croker
    Dec 6, 2018 at 12:19

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.