Moving data between middleware, container and views

Hi there,
I’m looking to find a good way to moev data between middleware, container and views (PHP-View).

For example, site has a form that changes language, $_POST is read on middleware and I would need this language variable also on container and views. Is it a bad practise to just be ugly and do something like:

// middleware
// handle and verify $_POST[‘language’] and then…
$this->language = $language;

// container
$view->language = $c->language;

// view
// access selected language
echo $this->language;

Or what’s the recommended way to do something like this?


You would write the language setting to a session.

Then pull it from the session and add it to your templating layer.

$_SESSION['lang'] = filtered($_POST['lang']);
$lang = $_SESSION['lang'];

$view->render(response, $lang."/mytemplate.twig");

1 Like

Thanks. So if I need to access current language in template, i.e. form select where current language needs to be selected, I just use session variable inside view file or should it be injected in the view object?

Is it commonly bad practise to create variables or arrays or whatever custom stuff inside app object (like $app->myVar = ‘whatever’)?

Yes this is generally considered bad practice… you really want to look passing these things around using Dependency injection and not service location.

1 Like