jeudi 4 février 2021

Service depending on route parameter or multiple static routes

Example I have one route: product/{product_type}/{product_id}

Which each product_type I use one service to process it, something like this in action:

// action of above route
public function proccessProduct($productType, $productId)
{
    switch ($prodctType) {
        case 'book':
            $service = $this->bookService;
            break;
        case 'mobilephone':
           $service = $this->mobilePhoneService;
           break;
        //adding more case if there is more product type
    }

    $service->process($productId);
}

And other approach, I write multiple route:

route 1: product/book/{product_id}
route 2: product/mobilephone/{product_id}
...

and $bookService is used in route 1, $mobilePhoneService is used in route 2, ... We can avoid switch case, but we have to write multiple routes.

I don't know which approach is better?

Aucun commentaire:

Enregistrer un commentaire