# Using same class for multiple resources in zend expressive Hal

**URL:** https://discourse.laminas.dev/t/using-same-class-for-multiple-resources-in-zend-expressive-hal/1058
**Category:** Mezzio
**Tags:** zend-expressive-hal
**Created:** [May 8, 2019, 6:07pm UTC](https://discourse.laminas.dev/t/using-same-class-for-multiple-resources-in-zend-expressive-hal/1058 "2019-05-08T18:07:43Z")
**Posts on this page:** 2
**Page:** 1

<div class="post-metadata">

### Author: ![aizaz\_akhtar](https://yyz2.discourse-cdn.com/flex032/user_avatar/discourse.laminas.dev/aizaz_akhtar/32/487_2.png) [@aizaz\_akhtar](https://discourse.laminas.dev/u/aizaz_akhtar)
#### Post date: [May 8, 2019, 6:07pm UTC](https://discourse.laminas.dev/t/using-same-class-for-multiple-resources-in-zend-expressive-hal/1058/1 "2019-05-08T18:07:43Z")

</div>

I am using Zend Expressive Hal to generate a Hal Response. The problem is that when I use the same resource\_class for 2 routes in Hal metadata, it throws an exception and does not work. I was wondering if there is a way where I can use the same object for more than one route. Here is my Hal metadata code:

```php
return [
     MetadataMap::class => [
        [
            ' __class__' => RouteBasedResourceMetadata::class,
            'resource_class' => ImmutablePropertyObject::class,
            'route' => 'api.ping',
            'extractor' => ArraySerializableHydrator::class,
        ],
         [
             ' __class__' => RouteBasedResourceMetadata::class,
             'resource_class' => \Abstracts\PropertyObject::class,
             'route' => 'api.getEventById',
             'extractor' => \Event\EventHydrator::class,
         ],
         [
             ' __class__' => RouteBasedResourceMetadata::class,
             'resource_class' => \Abstracts\PropertyObject::class,
             'route' => 'api.postEvent',
             'extractor' => \Event\EventHydrator::class,
         ]
    ]
];

```

Here are my routes:

```php
return function (Application $app, MiddlewareFactory $factory, ContainerInterface $container) : void {
    $app->get('/', App\Handler\HomePageHandler::class, 'home');
    $app->get('/api/ping', App\Handler\PingHandler::class, 'api.ping');
    $app->get('/api/event/:id', Event\Handler\EventReadHandler::class, 'api.getEventById');
    $app->post('/api/event', Event\Handler\EventPostHandler::class, 'api.postEvent');
    $app->put('/api/event', Event\Handler\EventUpdateHandler::class, 'api.updateEvent');

};

```

---

<div class="post-metadata">

### Author: ![matthew](https://yyz2.discourse-cdn.com/flex032/user_avatar/discourse.laminas.dev/matthew/32/14_2.png) [@matthew](https://discourse.laminas.dev/u/matthew)
#### Post date: [May 8, 2019, 6:52pm UTC](https://discourse.laminas.dev/t/using-same-class-for-multiple-resources-in-zend-expressive-hal/1058/2 "2019-05-08T18:52:58Z")

</div>

This is by design. Why? Because a given _resource_ should have only _one_ associated endpoint. It can be _embedded_ in other resources, but it should only have one canonical URL associated.

In your example above, you don’t need to define a resource for the `api.postEvent` route; when that route is called, you will be creating a `PropertyObject`, and the representation will use the resource for the `api.getEventById` route, which will generate the canonical URL for the resource.

If you absolutely need a different URL to be used, _extend_ the original class and map that extension class. However, based on what you have above, I’d remove the resource associated with the `api.postEvent` as it’s redundant.
