Giter Club home page Giter Club logo

laravel-negotiator's Introduction

Content negotiation for Laravel API

Build Status Latest Stable Version License

Installation

Require this package with composer:

composer require mleczek/laravel-negotiator

In config/app.php add the NegotiatorServiceProvider:

'providers' => [
    Mleczek\Negotiator\NegotiatorServiceProvider::class,
]

Usage

Package provide negotiate macro for ResponseFactory:

public function show(User $user)
{
    return response()->negotiate($user);
}

The same result can be achieved using Mleczek\Negotiator\ContentNegotiation class:

public function __construct(ContentNegotiation $cn)
{
    $this->cn = $cn;
}

public function show(Request $request, User $user)
{
    return $cn->negotiate($request, $user);
}

For both negotiate method and macro there is also one parameter which override result for specified content types:

public function show(User $user)
{
    // Return static JSON for request which
    // contains "application/json" in "Accepts" header.
    return response()->negotiate($user, [
        'application/json' => '{"id":4}',
    ]);
}

By default package support application/json and application/xml. XML format is resolved using mleczek/xml package.

Extending

You can extend supported content types in boot method of any of your ServiceProvider:

public function boot(ContentNegotiation $negotiator)
{
    // The ContentNegotiation facade is also available
    $negotiator->extend('application/json', function () {
        return new JsonHandler();
    });
}

The first parameter accept content type (or array of content types) for which the specified handler should be used.

Handler must implement the Mleczek\Negotiator\Contracts\ContentNegotiationHandler interface.

Contributing

Thank you for considering contributing! If you would like to fix a bug or propose a new feature, you can submit a Pull Request.

License

The library is licensed under the MIT license.

laravel-negotiator's People

Contributors

mleczek avatar

Stargazers

 avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.