PHP code example of gofabian / negotiation-middleware

1. Go to this page and download the library: Download gofabian/negotiation-middleware library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

gofabian / negotiation-middleware example snippets


$app->add(new NegotiationMiddleware(
    [ 'accept-language' => ['en', 'de-DE'] ],
    false // 406 status for empty accept headers
));

$app->add(new NegotiationMiddleware(
    [ 'accept' => ['text/xml'] ],
    true,
    'foo' // custom name
));

$app->get('/example', function($request, $response, $args) {
    $result = $request->getAttribute('foo');
    return $response->write($result->getMediaType());
});

$result = $request->getAttribute('negotiation');

$result->getMediaType();  // these
$result->getLanguage();   // getters
$result->getCharset();    // return
$result->getEncoding();   // strings

$result = $request->getAttribute('negotiation');

$result->getAccept();          // results
$result->getAcceptLanguage();  // from
$result->getAcceptCharset();   // negotiation
$result->getAcceptEncoding();  // library
 php

use Gofabian\Negotiation\NegotiationMiddleware;

// create Slim app
$app = new \Slim\App;

// configure middleware
$app->add(new NegotiationMiddleware([
    'accept' => ['text/html', 'application/json']
]));

// use negotiated media type
$app->get('/mediatype', function ($request, $response, $args) {
    $negotiation = $request->getAttribute('negotiation');
    $mediaType = $negotiation->getMediaType();
    return $response->write("media type = " . $mediaType);
});

// run app
$app->run();
 php
$app->add(new NegotiationMiddleware([
    'accept' => ['text/html', 'application/json'],
    'accept-language' => ['en', 'de-DE'],
    'accept-encoding' => ['gzip'],
    'accept-charset' => ['utf-8', 'ascii']
]));