# How best to pass parameters to every page?

**URL:** <https://discourse.slimframework.com/t/how-best-to-pass-parameters-to-every-page/537>\
**Category:** Questions\
**Created:** [July 21, 2016, 12:42pm UTC](https://discourse.slimframework.com/t/how-best-to-pass-parameters-to-every-page/537 "2016-07-21T12:42:15Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Antnee](https://avatars.discourse-cdn.com/v4/letter/a/919ad9/32.png) [@Antnee](https://discourse.slimframework.com/u/Antnee)\
**Post date:** [July 21, 2016, 12:42pm UTC](https://discourse.slimframework.com/t/how-best-to-pass-parameters-to-every-page/537/1 "2016-07-21T12:42:15Z")

</div>

For example, let’s say that you need to put a link in the footer of every page, but it’s different depending on certain criteria that are determined in middleware:

```php
<?php
use Psr\Http\Message\ServerRequestInterface as Request;
use Psr\Http\Message\ResponseInterface as Response;

$app->add(function (Request $request, Response $response, callable $next) : Response
{
    $ua = $request->getHeader('user-agent');
    $isAndroid = (strpos($ua[0], 'Android') !== false);
    $request = $request->withAttribute('isAndroid', $isAndroid);

    return $next($request, $response);
});

```

This works and if I check the `$request` object in my actions I can see that the attribute `isAndroid` is `true` or `false`

The problem is that I need to get this into EVERY page, so I don’t want to put it in every single action.

I’m building on top of @akrabat’s Slim Skeleton so figured I’d be able to do something like this:

```php
<?php
use Interop\Container\ContainerInterface;
use Slim\Views\Twig;
use Slim\Views\TwigExtension;

$container = $app->getContainer();

$container['view'] = function (ContainerInterface $c) : Twig
{
    $settings = $c->get('settings')['view'];
    $view = new Twig($settings['template_path'], $settings['twig']);
    $view->addExtension(new TwigExtension($c->get('router'), $c->get('request')->getUri()));
    $view->addExtension(new Twig_Extension_Debug());
    $view['isAndroid'] = $c->get('request')->getAttribute('isAndroid');

    return $view;
};

```

Of course that `$request` object will be a new one though, so it doesn’t include the attributes set in Middleware and I shouldn’t use that anyway…

So, short of having to add it in every action, what’s a good way to get this attribute into every view?

Thanks

---

<div class="post-metadata">

**Author:** ![geggleto](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.slimframework.com/geggleto/32/11_2.png) [@geggleto](https://discourse.slimframework.com/u/geggleto)\
**Post date:** [July 21, 2016, 1:01pm UTC](https://discourse.slimframework.com/t/how-best-to-pass-parameters-to-every-page/537/2 "2016-07-21T13:01:41Z")

</div>

You add a twig global environment variable.

```php
//...
$view = new Twig($settings['template_path'], $settings['twig']);
//...
$view->getEnvironment()->addGlobal("myvariable", 'myvalue');

```

---

<div class="post-metadata">

**Author:** ![Antnee](https://avatars.discourse-cdn.com/v4/letter/a/919ad9/32.png) [@Antnee](https://discourse.slimframework.com/u/Antnee)\
**Post date:** [July 21, 2016, 1:07pm UTC](https://discourse.slimframework.com/t/how-best-to-pass-parameters-to-every-page/537/3 "2016-07-21T13:07:48Z")

</div>

How does that get it from the Middleware though? I use that attribute in some other places as well and didn’t really want to use Twig as a container to pass values around. I was hoping to use the `$request` object. My previous solution does the same thing (ie setting `$view['myvariable'] = 'myvalue';`)

---

<div class="post-metadata">

**Author:** ![geggleto](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.slimframework.com/geggleto/32/11_2.png) [@geggleto](https://discourse.slimframework.com/u/geggleto)\
**Post date:** [July 21, 2016, 1:58pm UTC](https://discourse.slimframework.com/t/how-best-to-pass-parameters-to-every-page/537/4 "2016-07-21T13:58:38Z")

</div>

Oh my bad…

you need to pass the twig view into your middleware using some form of dependency injection.

---

<div class="post-metadata">

**Author:** ![Antnee](https://avatars.discourse-cdn.com/v4/letter/a/919ad9/32.png) [@Antnee](https://discourse.slimframework.com/u/Antnee)\
**Post date:** [July 21, 2016, 3:09pm UTC](https://discourse.slimframework.com/t/how-best-to-pass-parameters-to-every-page/537/5 "2016-07-21T15:09:03Z")

</div>

Sorry, you’ve lost me now. I thought the function signature for the middleware was fixed to receive a `$request`, `$response` and callable `$next` before the route executes? How can I get Twig into the middleware?

---

<div class="post-metadata">

**Author:** ![JoeBengalen](https://avatars.discourse-cdn.com/v4/letter/j/b5e925/32.png) [@JoeBengalen](https://discourse.slimframework.com/u/JoeBengalen)\
**Post date:** [July 21, 2016, 3:15pm UTC](https://discourse.slimframework.com/t/how-best-to-pass-parameters-to-every-page/537/6 "2016-07-21T15:15:39Z")

</div>

You could set it into the view from the middleware itself.

```php
$app->add(function (Request $request, Response $response, callable $next) : Response
{
    $ua = $request->getHeader('user-agent');
    $isAndroid = (strpos($ua[0], 'Android') !== false);

    $view = $this->get('view'); // note that $this === ContainerInterface
    $view->getEnvironment()->addGlobal("isAndroid", $isAndroid);

    return $next($request, $response);
});

```

---

<div class="post-metadata">

**Author:** ![Antnee](https://avatars.discourse-cdn.com/v4/letter/a/919ad9/32.png) [@Antnee](https://discourse.slimframework.com/u/Antnee)\
**Post date:** [July 21, 2016, 3:40pm UTC](https://discourse.slimframework.com/t/how-best-to-pass-parameters-to-every-page/537/7 "2016-07-21T15:40:34Z")

</div>

> [@JoeBengalen](#):
>
> $view = $this-\>get(‘view’); // note that $this === ContainerInterface  
> $view-\>getEnvironment()-\>addGlobal(“isAndroid”, $isAndroid);

OK, **THIS** I like! This works perfectly! Thanks! I’d completely forgotten about being able to retrieve the container from within the middleware. Thank you so much
