Hi,
I am using multiple controller methods for my actions and I need to share variables between them which are supplied by the user at a certain point in time. Later this input is needed for my dependencies in container.
I don’t want to use sessions or globals as this information may be sensitive. What is the best way to permanently change app settings dynamically so these can be then accessed from other controller method later. The below code does not work as it appears that everytime a route/controller method is called, the default settings overwrite whatever I have modified previously.
$settings = $container->get('settings');
$settings->replace([
'user_input' => 'my_sensitive_info',
]);
My reduced app code:
index.php
$settings = [
'user_input'=>null
]
$app = new \Slim\App($settings);
require dependencies.php;
...
$app->run();
dependencies.php
// Configuration for Slim Dependency Injection Container
$container = $app->getContainer();
// My service in container to which user_input should be set
$container['my_service'] = function ($c) {
return new MyService()
};
myservice.php
Class MyService {
public $user_input;
function setUserInput($user_input){
$this->user_input = $user_input;
}
}
controller.php
//inherits container instance including my_service from BaseController
Class MyControler extends BaseController {
public function login(Request $request, Response $response, $args)
{
$user_input = $request->getParsedBody();
//how to pass $user_input to the settings permanently
.....
//or how to modify $container['my_instance'] permanently with setUserInput($user_input)
.....
}
Thank you for any hints,
I realise my architecture may not be great, but I guess it is often needed to initialize a dependency with dynamic input. I could initialize the dependency in every route, but that seems very annoying.