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