Getting parameter from URL for REST Service

I am using API Tools for developing REST APIs. My URL is
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.

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.

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 …

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]. 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.

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. :wink:

As suggested I have created new topic. Thanks