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;

    }
}
up vote 2 down vote accepted

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

MyPlugin::getInstance()->myService->checkCommerceRecord($model);
  • 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 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 at 12:19
  • awesome, that works :) thanks so much – HendrikEng Dec 6 at 12:25

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.