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,
],
];
}
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->postAjax($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
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.