Params vs. getAttribute

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');
});

I suppose if you were implementing a different Route Strategy 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.

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);
	}
}