# How to load different methods inside class with multiple route

**URL:** <https://discourse.laminas.dev/t/how-to-load-different-methods-inside-class-with-multiple-route/1950>\
**Category:** Mezzio\
**Created:** [November 17, 2020, 2:18pm UTC](https://discourse.laminas.dev/t/how-to-load-different-methods-inside-class-with-multiple-route/1950 "2020-11-17T14:18:39Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Blacktornado](https://yyz2.discourse-cdn.com/flex032/user_avatar/discourse.laminas.dev/blacktornado/32/815_2.png) [@Blacktornado](https://discourse.laminas.dev/u/Blacktornado)\
**Post date:** [November 17, 2020, 2:18pm UTC](https://discourse.laminas.dev/t/how-to-load-different-methods-inside-class-with-multiple-route/1950/1 "2020-11-17T14:18:39Z")

</div>

Hello ! I am very new to Laminas and facing a simple problem. Currently I am loading `handle()` from `class SearchInquiryHandler` when routed as `$app->post('/api/search/inquiry', App\Handler\SearchInquiryHandler::class, 'api.search.inquiry');` from `routes.php`. I want to load another method from `class SearchInquiryHandler` but no luck. Here is the basic script. yes i have a `SearchInquiryHandlerFactory.php` and `ConfigProvider.php`

here is my `routes.php` this is loading the `handle()` inside `SearchInquiryHandler

```
$app->post('/api/search/inquiry', App\Handler\SearchInquiryHandler::class, 'api.search.inquiry');

```

Here is my `SearchInquiryHandler.php`

```
class SearchInquiryHandler implements RequestHandlerInterface {
    public function __construct(##SOME CODE HERE##) {
        <!--SOME CODE HERE-->
    }

    public function handle(ServerRequestInterface $request) : ResponseInterface
    {
        return new JsonResponse(['ack' => 'GETTING RESPONSE']);
    }
    public function loadThisMethod(ServerRequestInterface $request) : ResponseInterface
    {
        return new JsonResponse(['ack' => 'want to load this method']);
    }
}

```

How can i route to `'SearchInquiryHandler:loadThisMethod'`  
with different route definition loading same class like  
`$app->post('/api/search/loadthismethod', App\Handler\SearchInquiryHandler::class, 'api.search.inquiry');`

I am thinking like if i can detect routing URL inside `handle()` and with proper checking i can load `loadthismethod()`

inside `handle()` method something like this may work

```
public function handle(ServerRequestInterface $request) : ResponseInterface
{
    $uri = $request->getUri();
    $path = $uri->getPath();

    if($path!='/api/search/inquiry'){
        $this->loadthismethod();
    }
}

```

Any guidance will be hugely appreciated

---

<div class="post-metadata">

**Author:** ![Blacktornado](https://yyz2.discourse-cdn.com/flex032/user_avatar/discourse.laminas.dev/blacktornado/32/815_2.png) [@Blacktornado](https://discourse.laminas.dev/u/Blacktornado)\
**Post date:** [November 17, 2020, 7:50pm UTC](https://discourse.laminas.dev/t/how-to-load-different-methods-inside-class-with-multiple-route/1950/2 "2020-11-17T19:50:09Z")

</div>

Ok got it ! firstly I was not passing `$request` to `$this->loadthismethod($request);` inside `handle()`

Secondly if you wish to make multiple route pointing same  
`Class SearchInquiryHandler` within `handle()`

you can specify the routes giving unique names

`$app->post('/api/search/inquiry', App\Handler\SearchInquiryHandler::class, 'api.search.inquiry');`

`$app->get('/api/search/test', App\Handler\SearchInquiryHandler::class, 'api.search.test');`

and inside `handle()` you can check routing URL and load different methods as per needs

```
$uri = $request->getUri();
$path = $uri->getPath(); 

if($path!='/api/search/inquiry'){
    return $this->loadthismethod($request);
}else{
    return new HtmlResponse($this->template->render('app::search-inquiryresult', [
			'entry' => $somedata,
		]));
}

```

OR if you are only going to have only one http request method like GET or POST to all the methods of a single class, you can do something like this

`$app->get('/api/search[/{action:inquiry|test}[/{id}]]', App\Handler\SearchInquiryHandler::class, 'api.search.inquiry',['GET']);`

I am sure there are better ways to deal this

---

<div class="post-metadata">

**Author:** ![acelaya](https://yyz2.discourse-cdn.com/flex032/user_avatar/discourse.laminas.dev/acelaya/32/908_2.png) [@acelaya](https://discourse.laminas.dev/u/acelaya)\
**Post date:** [November 21, 2020, 4:43pm UTC](https://discourse.laminas.dev/t/how-to-load-different-methods-inside-class-with-multiple-route/1950/3 "2020-11-21T16:43:25Z")

</div>

What you’d usually want is to create different handlers for every route, each one of them with its own handle method.
