# Getting parameter from URL for REST Service

**URL:** <https://discourse.laminas.dev/t/getting-parameter-from-url-for-rest-service/2624>\
**Category:** API Tools\
**Created:** [November 30, 2021, 5:44pm UTC](https://discourse.laminas.dev/t/getting-parameter-from-url-for-rest-service/2624 "2021-11-30T17:44:54Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![sanjivsharmalv](https://yyz2.discourse-cdn.com/flex032/user_avatar/discourse.laminas.dev/sanjivsharmalv/32/1164_2.png) [@sanjivsharmalv](https://discourse.laminas.dev/u/sanjivsharmalv)\
**Post date:** [November 30, 2021, 5:44pm UTC](https://discourse.laminas.dev/t/getting-parameter-from-url-for-rest-service/2624/1 "2021-11-30T17:44:54Z")

</div>

I am using API Tools for developing REST APIs. My URL is  
[my-website.com/v1/customers/1/orders](http://my-website.com/v1/customers/1/orders)

I want to display orders for the customer with customer\_id = 1.

My question is:  
-in general how do I implement this functionality  
-Nonetheless my approach is one of creating service without using DB-Connect service. I plan to use use auto-generated OrdersResource. If this is the right approach then how do I get the customer\_id in OrdersResource. I tried to use params plugin but there also faced the error.

---

<div class="post-metadata">

**Author:** ![ezkimo](https://yyz2.discourse-cdn.com/flex032/user_avatar/discourse.laminas.dev/ezkimo/32/781_2.png) [@ezkimo](https://discourse.laminas.dev/u/ezkimo)\
**Post date:** [December 1, 2021, 11:58am UTC](https://discourse.laminas.dev/t/getting-parameter-from-url-for-rest-service/2624/2 "2021-12-01T11:58:43Z")

</div>

The Laminas API Tools uses the laminas/laminas-router package. This allows you to define segmented routes with placeholders as described in the laminas [documentation for routing](https://docs.laminas.dev/laminas-router/routing/).

A good API maps only the CRUD (create, read, update and delete) operations via REST. Everything special that does not match CRUD operations I 'd do with a specialized RPC service. To keep it clean and simple just use one controler for one action per route to avoid oversized controllers. To be completely logical about this, I would suggest that you implement different services using the API tools. A REST service that handles the customer issues. A REST service that handles the concerns for the orders. A RPC service that delivers the orders of a specific customer.

In your module.config.php …

```auto
return [
    ...
    'router' => [
        'application.rpc.customer-orders' => [
            'type' => 'Segment',
            'options' => [
                'route' => '/customer/orders[/:customer_id]',
                'defaults' => [
                    'controller' => 'Application\\V1\\Rpc\\CustomerOrders\\Controller',
                    'action' => 'listAction',
                ],
            ],
        ],
    ],
    ...
];

```

As you can see the route is defined as [https://www.example.tld/customer/orders[/:customer\_id]](https://www.example.tld/customer/orders%5B/:customer_id%5D). In this case `[/:customer_id]` is the placeholder for any id. If given, the parameter is forwarded by a slash.

In your controller …

```
class CustomerOrdersController extends AbstractActionController
{
    public function listAction()
    {
        $customerId = $this->params()->fromRoute('customer_id', 0);
    }
}

```

To get the given ID from the the route, you can use the parameter controller plugin, which is automatically active in any controller. In this example you fetch the customer id from route, where we defined `customer_id` as route parameter. The second argument is a default fallback argument, which will be used, when there 's no customer given. As a result of this, you can check, if the customer id is 0 and get some error reporting done.

Summary: Define your route in the `module.config.php` file and grab the defined parameters with the params controller plugin.

---

<div class="post-metadata">

**Author:** ![ezkimo](https://yyz2.discourse-cdn.com/flex032/user_avatar/discourse.laminas.dev/ezkimo/32/781_2.png) [@ezkimo](https://discourse.laminas.dev/u/ezkimo)\
**Post date:** [December 3, 2021, 10:56am UTC](https://discourse.laminas.dev/t/getting-parameter-from-url-for-rest-service/2624/4 "2021-12-03T10:56:05Z")

</div>

Hey Sanjivsharmalv,

would you do me a favor and open a new thread for your new question? The original question has been solved. Your new question has relatively little to do with the original question. In order to provide an easy-to-find solution to as many forum users as possible, it would make more sense for you to post the question in a separate thread. This way users who have the same problem can find the answer faster.

I even have an answer to your new problem. 😉

---

<div class="post-metadata">

**Author:** ![sanjivsharmalv](https://yyz2.discourse-cdn.com/flex032/user_avatar/discourse.laminas.dev/sanjivsharmalv/32/1164_2.png) [@sanjivsharmalv](https://discourse.laminas.dev/u/sanjivsharmalv)\
**Post date:** [December 3, 2021, 11:03am UTC](https://discourse.laminas.dev/t/getting-parameter-from-url-for-rest-service/2624/5 "2021-12-03T11:03:57Z")

</div>

As suggested I have created new topic. Thanks
