# Pass Object through Routes

**URL:** https://discourse.slimframework.com/t/pass-object-through-routes/1495
**Category:** Questions
**Created:** [May 16, 2017, 4:05pm UTC](https://discourse.slimframework.com/t/pass-object-through-routes/1495 "2017-05-16T16:05:02Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![Ramyhakam](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.slimframework.com/ramyhakam/32/195_2.png) [@Ramyhakam](https://discourse.slimframework.com/u/Ramyhakam)
#### Post date: [May 16, 2017, 4:05pm UTC](https://discourse.slimframework.com/t/pass-object-through-routes/1495/1 "2017-05-16T16:05:02Z")

</div>

I am using Slim framework With parse server php SDK

I want to pass current user object after user login the parse server How I can retrieve this object in any routes in Slim $app? So I can identify the user

---

<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: [May 16, 2017, 8:07pm UTC](https://discourse.slimframework.com/t/pass-object-through-routes/1495/2 "2017-05-16T20:07:02Z")

</div>

I would do that with [Middleware](https://www.slimframework.com/docs/concepts/middleware.html). It might look something like this.

```php
<?php

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

/**
 * Example middleware to attach user to the Request Object
 *
 * @param \Psr\Http\Message\ServerRequestInterface $request PSR7 request
 * @param \Psr\Http\Message\ResponseInterface $response PSR7 response
 * @param callable $next Next middleware
 *
 * @return \Psr\Http\Message\ResponseInterface
 */
public function __invoke(Request $request, Response $response, $next) {
    $user = 'user'; // user object here instead of 'user'
    $request = $request->withAttribute('user', $user);

    $response = $next($request, $response);

    return $response;
};

```

Then the user object becomes part of the request object which will be passed along to any controller or action classes you may have.

---

<div class="post-metadata">

### Author: ![Ramyhakam](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.slimframework.com/ramyhakam/32/195_2.png) [@Ramyhakam](https://discourse.slimframework.com/u/Ramyhakam)
#### Post date: [May 17, 2017, 11:59am UTC](https://discourse.slimframework.com/t/pass-object-through-routes/1495/3 "2017-05-17T11:59:19Z")

</div>

This can work But,if I use this i need to login in parse server every request to get the current user object ?

---

<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: [May 17, 2017, 12:32pm UTC](https://discourse.slimframework.com/t/pass-object-through-routes/1495/4 "2017-05-17T12:32:05Z")

</div>

Yes, but there are other options. I will add the user object (or a portion thereof) to the session to avoid trips back to the database. That could work for you as well.

---

<div class="post-metadata">

### Author: ![Ramyhakam](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.slimframework.com/ramyhakam/32/195_2.png) [@Ramyhakam](https://discourse.slimframework.com/u/Ramyhakam)
#### Post date: [May 17, 2017, 12:50pm UTC](https://discourse.slimframework.com/t/pass-object-through-routes/1495/5 "2017-05-17T12:50:58Z")

</div>

starting the session in the top of index.php file solve the problem  
I can now access the Current user object from parse server php sdk

Thanks 😌
