# Params vs. getAttribute

**URL:** <https://discourse.slimframework.com/t/params-vs-getattribute/967>\
**Category:** Questions\
**Created:** [November 30, 2016, 3:49pm UTC](https://discourse.slimframework.com/t/params-vs-getattribute/967 "2016-11-30T15:49:05Z")\
**Posts on this page:** 3\
**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:** [November 30, 2016, 3:49pm UTC](https://discourse.slimframework.com/t/params-vs-getattribute/967/1 "2016-11-30T15:49:05Z")

</div>

Just out of curiosity, what’s the difference between the following:

```
$params['id']
$request->getAttribute('id')

```

They both output the same value but I wanted to know if there’s a preference or reason to use one over the other?

```
$app->get('/user/{id}',function($request,$response,$params) {
  echo 'ID: ' . $params['id'] . '/' . $request->getAttribute('id');
});
```

---

<div class="post-metadata">

**Author:** ![tflight](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.slimframework.com/tflight/32/1205_2.png) [@tflight](https://discourse.slimframework.com/u/tflight)\
**Post date:** [November 30, 2016, 4:58pm UTC](https://discourse.slimframework.com/t/params-vs-getattribute/967/2 "2016-11-30T16:58:33Z")

</div>

I suppose if you were implementing a different [Route Strategy](https://www.slimframework.com/docs/objects/router.html#route-strategies) you might wish to access the route placeholder from the request object. I suppose you might also wish to do something with the `$params` in middleware and pass the updated values to the request object.

It might also have something to do with still being able to obtain the values if you are swapping out Slim’s default PSR-7 Request for something else?

But most of the time I see people using `$params` for route placeholders and `$request->getAttribute(...)` for get/post values.

---

<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:** [November 30, 2016, 6:15pm UTC](https://discourse.slimframework.com/t/params-vs-getattribute/967/3 "2016-11-30T18:15:37Z")

</div>

I like the idea of using $params as a reference to placeholders in the route. Seems a bit more explicit and reserving getAttribute for applying values to the response, such as a post request:

```
$app->get('/signup','Controller:emailForm');
$app->post('/signup','Controller:postEmailForm');

class Controller {

	function emailForm($request,$response) {
		$data = array(
			'templateContent' => 'emailForm.html',
			'emailError' => $request->getAttribute('emailError')
		);
		return $this->view->render($response,'template.html',$data);
	}

	function postEmailForm($request,$response) {
		$postData = $request->getParsedBody();

		if (!filter_var($postData['email'],FILTER_VALIDATE_EMAIL)) {
			$request = $request->withAttribute('emailError','That email is invalid.');
		}

		return $this->emailForm($request,$response);
	}
}
```
