How to load different methods inside class with multiple route

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

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

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

2 Likes