如何解决使用函数作为参数
我有课
class Contact {
public function searchByPhone(ServerRequestInterface $request,BaseResponse $response,$args) {
return 'somedata';
}
}
Slim 框架使用这种方法
$app->get("/searchByPhone/2342",Contact::class . ':searchByPhone');
我要使用
$app->get("/searchByPhone/2342",Contact::searchByPhone());
这样,当我单击某个函数时,NetBeans 会将我带到代码中的正确位置。 我该怎么做?
解决方法
您可以尝试使用带有单个 __invoke 方法的 Single Action Controller。
示例
<?php
namespace App\Action;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
final class ContactPhoneSearchAction
{
public function __invoke(ServerRequestInterface $request,ResponseInterface $response): ResponseInterface {
$params = (array)$request->getQueryParams();
$phone = (string)$params['phone'];
// Invoke service method
// ...
// Build response
$response->getBody()->write('Hello,World!');
return $response;
}
}
用法
$app->get('/contacts/phone',\App\Action\ContactPhoneSearchAction::class);
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。