简体   繁体   English

在 laminas mezzio 中从具有多个路由的单个类加载不同的方法

[英]load different methods from a single class with multiple routes in laminas mezzio

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.现在我只能设法在SearchInquiryHandler类中加载handle() How can i also load loadThisMethod() inside SearchInquiryHandler from a different route.我怎么能还加载loadThisMethod()SearchInquiryHandler从不同的路线。 I am thinking if i can detect the routing url and with proper checking i can load loadThisMethod() from my handle() method.我在想我是否可以检测到路由 url 并通过适当的检查我可以从我的handle()方法加载loadThisMethod() Any guidance will be hugely helpful.任何指导都会非常有帮助。

For a single route here is routes.php对于单个路由,这里是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这是我的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这是我的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这是我的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);首先我没有将 $request 传递给$this->loadthismethod($request); inside handle()内部handle()

Secondly if you wish to make multiple route pointing same Class SearchInquiryHandler within handle()其次,如果您希望在handle()中使多个路由指向同一Class SearchInquiryHandler

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 needshandle()内部,您可以检查路由 URL 并根据需要加载不同的方法

$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或者,如果您只对一个类的所有方法只有一个 http 请求方法,例如GETPOST ,您可以执行以下操作

$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我相信有更好的方法来解决这个问题

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM