获取资源名称和请求方法类型



如何在 Apigility 中获取资源名称? 我试过了
$route = $event->getRouteMatch();但是,如果资源名称包含多个名称,则将其拆分为 .

例如,如果名称为"employeeVerifySomething",则返回"employee.verify.something"?

我还需要获取请求类型以区分"获取和获取所有">

您可以在module.config.phponBootstratp()方法中赶上它们。查看以下方法

public function onBootstrap(MvcEvent $e)
{
$request = $e->getRequest();
// Get the request method
$method = $request->getMethod();
// Get the path according to your format
$path = $request->getUri()->getPath();
$parts = explode('/', $path);
if (!empty($parts)) {
foreach ($parts as $part) {
$words = preg_split("/((?<=[a-z])(?=[A-Z])|(?=[A-Z][a-z]))/", $part);
if (!empty($words)) {
$words = array_filter($words, function($value, $key){
return !empty($value);
}, ARRAY_FILTER_USE_BOTH);
$words = array_map('strtolower', $words);
}
$paths[] = join('.', $words);
}
}
// Here is the formatted path
$path = join("/", $paths);
echo $method; // Outputs the requested method for example, GET
echo $path; // Outputs employee.verify.something against employeeVerifySomething 
}

上述方法的$path变量将能够返回所需的格式化资源。例如,"hunkeyPunkey/munkeyDunkeyChunkey">将输出为

/hunkey.punkey/munkey.dunkey.chunkey

对于这一点:"我还需要获取请求类型以区分"获取&&获取所有">

对于全部获取,有条件,您可以覆盖所有获取方法

$client = new ZendHttpClient($uri);
$client->setMethod('GET');
$client->setParameterGet(['id'=>1]);
//Or
$client->setParameterGet([]);

对于获取一个或获取方法

在网址后添加 ID:

$uri.'/'.$id;

相关内容

最新更新