# Middleware get request attributes after route

**URL:** <https://discourse.slimframework.com/t/middleware-get-request-attributes-after-route/1672>\
**Category:** Questions\
**Created:** [July 12, 2017, 6:26pm UTC](https://discourse.slimframework.com/t/middleware-get-request-attributes-after-route/1672 "2017-07-12T18:26:30Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![robrothedev](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.slimframework.com/robrothedev/32/137_2.png) [@robrothedev](https://discourse.slimframework.com/u/robrothedev)\
**Post date:** [July 12, 2017, 6:26pm UTC](https://discourse.slimframework.com/t/middleware-get-request-attributes-after-route/1672/1 "2017-07-12T18:26:30Z")

</div>

I’m trying to use $request-\>getAttribute(‘title’) in middleware called after a route has returned a response. The route is assigning the title via $request-\>withAttribute(‘title’,‘Page Title’).

Example:

```
    $app = new Slim\App($appContainer);

	$app->get('/',function($request,$response) {
		$request = $request->withAttribute('title','Home page');
		return $response;
	});

	$mw = function($request,$response,$next) {
		$response = $next($request,$response);
		$title = $request->getAttribute('title');
		$response->write('Title: ' . $title);
		return $response;	
	};

	$app->add($mw)->run();

```

Is it even possible to assign data like this from a route request to middleware?

---

<div class="post-metadata">

**Author:** ![SVL](https://avatars.discourse-cdn.com/v4/letter/s/848f3c/32.png) [@SVL](https://discourse.slimframework.com/u/SVL)\
**Post date:** [July 12, 2017, 9:14pm UTC](https://discourse.slimframework.com/t/middleware-get-request-attributes-after-route/1672/2 "2017-07-12T21:14:03Z")

</div>

Have you looked at setArgument?

```
<?php

use \Psr\Http\Message\ServerRequestInterface as Request;
use \Psr\Http\Message\ResponseInterface as Response;
use \Slim\App as Slim;

require dirname( __DIR__ ) . '/../vendor/autoload.php';

$config['displayErrorDetails'] = true;
$config['determineRouteBeforeAppMiddleware'] = true;

$app = new Slim(['settings' => $config]);

$app->get('/', function($request, $response) {
    return $response;
})->setArgument('title', 'Home page');

$mw = function($request, $response, $next) {
    $response = $next($request, $response);

    $route = $request->getAttribute('route');
    $title = $route->getArgument('title');

    $response->write('Title: ' . $title);
    return $response;
};

$app->add($mw);

$app->run();
```

---

<div class="post-metadata">

**Author:** ![robrothedev](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.slimframework.com/robrothedev/32/137_2.png) [@robrothedev](https://discourse.slimframework.com/u/robrothedev)\
**Post date:** [July 13, 2017, 6:37pm UTC](https://discourse.slimframework.com/t/middleware-get-request-attributes-after-route/1672/3 "2017-07-13T18:37:12Z")

</div>

That does work, however, I was looking for something that I wouldn’t have to add to each route. I ended creating a class to inject into my controllers. After thinking about it, what I wanted to do didn’t really make sense to put that type of responsibility on the request.

---

<div class="post-metadata">

**Author:** ![akrabat](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.slimframework.com/akrabat/32/9_2.png) [@akrabat](https://discourse.slimframework.com/u/akrabat)\
**Post date:** [July 14, 2017, 8:44am UTC](https://discourse.slimframework.com/t/middleware-get-request-attributes-after-route/1672/4 "2017-07-14T08:44:14Z")

</div>

The request is not passed back up the chain you need so use something that has state. The most obvious choice is the `route` object’s `arguments`. Alternatively, you could use a custom header on the `response` that you then remove, or you could implement a custom Response object with an `attributes` property.
