# Custom response in custom class

**URL:** <https://discourse.slimframework.com/t/custom-response-in-custom-class/2151>\
**Category:** Questions\
**Created:** [January 9, 2018, 4:06pm UTC](https://discourse.slimframework.com/t/custom-response-in-custom-class/2151 "2018-01-09T16:06:41Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![nooballday](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.slimframework.com/nooballday/32/586_2.png) [@nooballday](https://discourse.slimframework.com/u/nooballday)\
**Post date:** [January 9, 2018, 4:06pm UTC](https://discourse.slimframework.com/t/custom-response-in-custom-class/2151/1 "2018-01-09T16:06:41Z")

</div>

Hey i’ve been trying slim for several days now i am able to make simple API like so

```
$app->get('/articles/getcategories', function (Request $request, Response $response, array $args) {
(new Category) -> getCategories($response);
});

```

one problem is when i want to send a custom response code to my end point from my class, in this case Category class it doesn’t work, i was able to send a response using this method :

```
$response -> getBody() -> write(json_encode($this -> makeObject(704, 'Something went wrong!')));

```

But when i look at the documentation :

```
$newResponse = $response->withStatus(302);
$data = array('name' => 'Rob', 'age' => 40);
$newResponse = $oldResponse->withJson($data, 201);

```

or like this :

```
return $response->withStatus(xxx);

```

it doesnt work, this is my custom class

```
 public function getCategories($response){

    $dbconn = (new db())->connect();
        //start db connection
    try {
        $stmt = $dbconn->prepare("SELECT category_id, category_name, category_description, category_type FROM article_category");

        $categories = $stmt->fetchAll(PDO::FETCH_ASSOC);
        $response->getBody()->write(json_encode($categories));

    } catch (PDOException $e){
        $response -> getBody() -> write(json_encode($this -> makeObject(704, 'Something went wrong!')));
    }

}

```

What am i doing wrong here?

---

<div class="post-metadata">

**Author:** ![odan](https://avatars.discourse-cdn.com/v4/letter/o/9de053/32.png) [@odan](https://discourse.slimframework.com/u/odan)\
**Post date:** [January 9, 2018, 4:36pm UTC](https://discourse.slimframework.com/t/custom-response-in-custom-class/2151/2 "2018-01-09T16:36:21Z")

</div>

You should return the $response object (and use the withJson method).

```php
return $response;

```

---

<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:** [January 9, 2018, 4:36pm UTC](https://discourse.slimframework.com/t/custom-response-in-custom-class/2151/3 "2018-01-09T16:36:50Z")

</div>

The `$response` object is immutable.

You need to return the `$response` object from `getCategories()` and then return that from your action.
