本文整理汇总了PHP中Cake\Network\Request::parseAccept方法的典型用法代码示例。如果您正苦于以下问题:PHP Request::parseAccept方法的具体用法?PHP Request::parseAccept怎么用?PHP Request::parseAccept使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Cake\Network\Request
的用法示例。
在下文中一共展示了Request::parseAccept方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: prefers
/**
* Determines which content-types the client prefers. If no parameters are given,
* the single content-type that the client most likely prefers is returned. If $type is
* an array, the first item in the array that the client accepts is returned.
* Preference is determined primarily by the file extension parsed by the Router
* if provided, and secondarily by the list of content-types provided in
* HTTP_ACCEPT.
*
* @param string|array $type An optional array of 'friendly' content-type names, i.e.
* 'html', 'xml', 'js', etc.
* @return mixed If $type is null or not provided, the first content-type in the
* list, based on preference, is returned. If a single type is provided
* a boolean will be returned if that type is preferred.
* If an array of types are provided then the first preferred type is returned.
* If no type is provided the first preferred type is returned.
* @see RequestHandlerComponent::setContent()
*/
public function prefers($type = null)
{
$acceptRaw = $this->request->parseAccept();
if (empty($acceptRaw)) {
return $this->ext;
}
$accepts = $this->response->mapType(array_shift($acceptRaw));
if (!$type) {
if (empty($this->ext) && !empty($accepts)) {
return $accepts[0];
}
return $this->ext;
}
$types = (array) $type;
if (count($types) === 1) {
if (!empty($this->ext)) {
return in_array($this->ext, $types);
}
return in_array($types[0], $accepts);
}
$intersect = array_values(array_intersect($accepts, $types));
if (empty($intersect)) {
return false;
}
return $intersect[0];
}
示例2: testParseAcceptInvalidSyntax
/**
* Test that parsing accept headers with invalid syntax works.
*
* The header used is missing a q value for application/xml.
*
* @return void
*/
public function testParseAcceptInvalidSyntax()
{
$request = new Request(['environment' => ['url' => '/', 'HTTP_ACCEPT' => 'text/html,application/xhtml+xml,application/xml;image/png,image/jpeg,image/*;q=0.9,*/*;q=0.8']], false);
$result = $request->parseAccept();
$expected = ['1.0' => ['text/html', 'application/xhtml+xml', 'application/xml', 'image/jpeg'], '0.9' => ['image/*'], '0.8' => ['*/*']];
$this->assertEquals($expected, $result);
}
示例3: _setExtension
/**
* Set the extension based on the accept headers.
* Compares the accepted types and configured extensions.
* If there is one common type, that is assigned as the ext/content type for the response.
* The type with the highest weight will be set. If the highest weight has more
* than one type matching the extensions, the order in which extensions are specified
* determines which type will be set.
*
* If html is one of the preferred types, no content type will be set, this
* is to avoid issues with browsers that prefer HTML and several other content types.
*
* @param \Cake\Network\Request $request The request instance.
* @param \Cake\Network\Response $response The response instance.
* @return void
*/
protected function _setExtension($request, $response)
{
$accept = $request->parseAccept();
if (empty($accept)) {
return;
}
$accepts = $response->mapType($accept);
$preferedTypes = current($accepts);
if (array_intersect($preferedTypes, ['html', 'xhtml'])) {
return;
}
$extensions = Router::extensions();
foreach ($accepts as $types) {
$ext = array_intersect($extensions, $types);
if ($ext) {
$this->ext = current($ext);
break;
}
}
}
示例4: _setExtension
/**
* Set the extension based on the accept headers.
* Compares the accepted types and configured extensions.
* If there is one common type, that is assigned as the ext/content type for the response.
* The type with the highest weight will be set. If the highest weight has more
* than one type matching the extensions, the order in which extensions are specified
* determines which type will be set.
*
* If html is one of the preferred types, no content type will be set, this
* is to avoid issues with browsers that prefer HTML and several other content types.
*
* @param \Cake\Network\Request $request The request instance.
* @param \Cake\Network\Response $response The response instance.
* @return void
*/
protected function _setExtension($request, $response)
{
$accept = $request->parseAccept();
if (empty($accept)) {
return;
}
$accepts = $response->mapType($accept);
$preferredTypes = current($accepts);
if (array_intersect($preferredTypes, ['html', 'xhtml'])) {
return;
}
$extensions = array_unique(array_merge(Router::extensions(), array_keys($this->config('viewClassMap'))));
foreach ($accepts as $types) {
$ext = array_intersect($extensions, $types);
if (!empty($ext)) {
$this->ext = current($ext);
break;
}
}
}