I am trying to route to a single class and load different methods within that class.
Right now i can only manage to load handle()
inside SearchInquiryHandler
class. How can i also load loadThisMethod()
inside SearchInquiryHandler
from a different route. I am thinking if i can detect the routing url and with proper checking i can load loadThisMethod()
from my handle()
method. Any guidance will be hugely helpful.
For a single route here is routes.php
return static function (Application $app, MiddlewareFactory $factory, ContainerInterface $container) : void {
$app->post('/api/search/inquiry', App\Handler\SearchInquiryHandler::class, 'api.search.inquiry');
}
here is my SearchInquiryHandler.php
class SearchInquiryHandler implements RequestHandlerInterface {
protected $template = null;
private $searchInquiry;
private $containerName;
public function __construct(TemplateRendererInterface $template, SearchInquiryresultForm $form, SearchInquiry $searchInquiry, $containerName) {
$this->template = $template;
$this->searchInquiry= $searchInquiry;
}
public function handle(ServerRequestInterface $request) : ResponseInterface
{
$result = $this->searchInquiry->setInquiryValues();
return new HtmlResponse($this->template->render('app::search-inquiryresult', [
'entry' => $result,
]));
}
<!-- HOW TO LOAD THIS METHOD FROM THE ROUTE -->
public function loadThisMethod(ServerRequestInterface $request) : ResponseInterface
{
return new JsonResponse(['ack' => time()]);
}
here is my SearchInquiryHandlerFactory.php
class SearchInquiryHandlerFactory {
public function __invoke(ContainerInterface $container) : RequestHandlerInterface
{
$dependency = $container->get(SearchInquiry::class);
$template = $container->get(LaminasViewRenderer::class);
$form = $container->get(SearchInquiryresultForm::class);
return new SearchInquiryHandler($template,$form,$dependency,$container);
}
}
here is my ConfigProvider.php
public function getDependencies() : array
{
return [
'invokables' => [
****Some Code here****
],
'factories' => [
Handler\SearchInquiryHandler::class => Handler\SearchInquiryHandlerFactory::class,
],
];
}