【问题标题】:Apigility get Resource name and request method typeApigility 获取资源名称和请求方法类型
【发布时间】:2017-12-13 23:44:41
【问题描述】:
如何在 Apigility 中获取资源名称?
我试过
$route = $event->getRouteMatch();
但如果资源名称包含多个名称,则将其拆分为 .
如果 reouece 名称为“employeeVerifySomething”,则返回“employee.verify.something”?
我还需要获取请求类型以区分获取“获取 && 获取所有”
【问题讨论】:
标签:
php
zend-framework2
laminas-api-tools
zend-framework3
【解决方案1】:
对于这一点:“我还需要获取请求类型以区分获取“获取 && 获取所有”
对于 fetch all,有条件,你可以覆盖 fetch all 方法
$client = new \Zend\Http\Client($uri);
$client->setMethod('GET');
$client->setParameterGet(['id'=>1]);
//Or
$client->setParameterGet([]);
用于获取一个或获取方法
在url后面加id:
$uri.'/'.$id;
【解决方案2】:
您可以在module.config.php 的onBootstratp() 方法中找到它们。看看下面的方法
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