# Zend expressive hal throwing exception when used from multiple routes

**URL:** https://discourse.laminas.dev/t/zend-expressive-hal-throwing-exception-when-used-from-multiple-routes/1059
**Category:** Mezzio
**Tags:** zend-expressive-hal
**Created:** [May 9, 2019, 12:52pm UTC](https://discourse.laminas.dev/t/zend-expressive-hal-throwing-exception-when-used-from-multiple-routes/1059 "2019-05-09T12:52:12Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![mmuhasan](https://yyz2.discourse-cdn.com/flex032/user_avatar/discourse.laminas.dev/mmuhasan/32/454_2.png) [@mmuhasan](https://discourse.laminas.dev/u/mmuhasan)
#### Post date: [May 9, 2019, 12:52pm UTC](https://discourse.laminas.dev/t/zend-expressive-hal-throwing-exception-when-used-from-multiple-routes/1059/1 "2019-05-09T12:52:12Z")

</div>

I have a model class `user` and I have three API end points related to this user object

1. POST /api/user
2. GET /api/user/:id\_user
3. PUT /api/user/

The POST and PUT has JSON body. I want to return the `user` object as HAL response for all three operations. My three handlers are looks like the following:

```
$userObj = $this->userService->relevent_service($params); // returning an user object
$request = $request->withAttribute("id_user", $userObj->getId());
$resource = $this->resourceGenerator->fromObject($eventObj,$request);
return $this->responseFactory->createResponse($request, $resource);

```

My halMetadataMap is like the following:

```
[
      ' __class__' => RouteBasedResourceMetadata::class,
      'resource_class' => user::class,
      'route' => 'api.getUserById',
      'extractor' => ArraySerializableHydrator::class,
]

```

It returns the right user HAL object with the GET route without any issue, but when I tried to generate the HAL object with POST and PUT route, the `ResourceGenerator` throws an exception.

If I create two additional proxy class for user class and create three separate metadata map for POST and PUT, it works without any issue. but it seems redundant. And the scenario I describe is not so uncommon. Therefore, what is the best practice to achieve this output?

---

<div class="post-metadata">

### Author: ![marcguyer](https://yyz2.discourse-cdn.com/flex032/user_avatar/discourse.laminas.dev/marcguyer/32/132_2.png) [@marcguyer](https://discourse.laminas.dev/u/marcguyer)
#### Post date: [May 9, 2019, 1:23pm UTC](https://discourse.laminas.dev/t/zend-expressive-hal-throwing-exception-when-used-from-multiple-routes/1059/2 "2019-05-09T13:23:04Z")

</div>

What’s the exception message? You might also find this enlightening:

[https://discourse.zendframework.com/t/using-same-class-for-multiple-resources-in-zend-expressive-hal/1058/2](https://discourse.zendframework.com/t/using-same-class-for-multiple-resources-in-zend-expressive-hal/1058/2)

---

<div class="post-metadata">

### Author: ![mmuhasan](https://yyz2.discourse-cdn.com/flex032/user_avatar/discourse.laminas.dev/mmuhasan/32/454_2.png) [@mmuhasan](https://discourse.laminas.dev/u/mmuhasan)
#### Post date: [May 9, 2019, 1:29pm UTC](https://discourse.laminas.dev/t/zend-expressive-hal-throwing-exception-when-used-from-multiple-routes/1059/3 "2019-05-09T13:29:09Z")

</div>

I saw your reply on that thread and I found my approach should work. But it throws an exception, missing `id_user` in my POST handler. as you can see, my POST handler url do not have the id\_user field. My routes are as follows:

```
$app->get('/api/user/:id_user', Event\Handler\UserReadHandler::class, 'api.getUserById');
$app->post('/api/user/', Event\Handler\UserWriteHandler::class, 'api.postUser');
$app->put('/api/user/', Event\Handler\UserUpdateHandler::class, 'api.putUser');

```

Looks like the `ResourceGenerator` is trying to fill out the :id\_user from the `$request`. I have adding that information with the `$request` as you can see, but this is not working yet.

---

<div class="post-metadata">

### Author: ![marcguyer](https://yyz2.discourse-cdn.com/flex032/user_avatar/discourse.laminas.dev/marcguyer/32/132_2.png) [@marcguyer](https://discourse.laminas.dev/u/marcguyer)
#### Post date: [May 9, 2019, 1:45pm UTC](https://discourse.laminas.dev/t/zend-expressive-hal-throwing-exception-when-used-from-multiple-routes/1059/4 "2019-05-09T13:45:29Z")

</div>

Did you try setting `id_user` attribute?

---

<div class="post-metadata">

### Author: ![mmuhasan](https://yyz2.discourse-cdn.com/flex032/user_avatar/discourse.laminas.dev/mmuhasan/32/454_2.png) [@mmuhasan](https://discourse.laminas.dev/u/mmuhasan)
#### Post date: [May 9, 2019, 1:47pm UTC](https://discourse.laminas.dev/t/zend-expressive-hal-throwing-exception-when-used-from-multiple-routes/1059/5 "2019-05-09T13:47:33Z")

</div>

I have tried with the following to inject the “id\_user” in the request. Looks like it is not working

```
$userObj = $this->userService->relevent_service($params); // returning an user object 
$request = $request->withAttribute("id_user", $userObj->getId()); 
$resource = $this->resourceGenerator->fromObject($eventObj,$request); 
return $this->responseFactory->createResponse($request, $resource);
```

---

<div class="post-metadata">

### Author: ![guidofaecke](https://yyz2.discourse-cdn.com/flex032/user_avatar/discourse.laminas.dev/guidofaecke/32/427_2.png) [@guidofaecke](https://discourse.laminas.dev/u/guidofaecke)
#### Post date: [May 15, 2019, 4:05am UTC](https://discourse.laminas.dev/t/zend-expressive-hal-throwing-exception-when-used-from-multiple-routes/1059/6 "2019-05-15T04:05:09Z")

</div>

One problem I see, your `put` doesn’t have an id, as in id\_user, in the route.  
The next problem might be, depending on your code (not sure what relevant\_service does), $userObj is null or -\>getId() returns null.
