LaravelPackages.net
Acme Inc.
Toggle sidebar
m1guelpf/laravel-multiformat

Multiformat Endpoints in Laravel

1.397
39
v1.0.1
About m1guelpf/laravel-multiformat

m1guelpf/laravel-multiformat is a Laravel package for multiformat endpoints in laravel. It currently has 39 GitHub stars and 1.397 downloads on Packagist (latest version v1.0.1). Install it with composer require m1guelpf/laravel-multiformat. Discover more Laravel packages by m1guelpf or browse all Laravel packages to compare alternatives.

Last updated

Multiformat Endpoints in Laravel

Latest Version on Packagist Total Downloads

Installation

You can install the package via composer:

composer require m1guelpf/laravel-multiformat

Usage

<?php

/**
 * Mark a route as 'multiformat' to allow different extensions (html, json, xml, etc.)
 *
 * This route will match all of these requests:
 *     /podcasts/4
 *     /podcasts/4.json
 *     /podcasts/4.html
 *     /podcasts/4.zip
 */
Route::get('/podcasts/{id}', 'PodcastsController@show')->multiformat();

/**
 * Use `Request::match()` to return the right response for the requested format.
 *
 * Supports closures to avoid doing unnecessary work, and returns 404 if the
 * requested format is not supported.
 *
 * Will also take into account the `Accept` header if no extension is provided.
 */
class PodcastsController
{
    public function show($id)
    {
        $podcast = Podcast::findOrFail($id);
        
        return request()->match([
            'html' => view('podcasts.show', [
                'podcast' => $podcast,
                'episodes' => $podcast->recentEpisodes(5),
            ]),
            'json' => $podcast,
            'xml' => function () use ($podcast) {
                return response($podcast->toXml(), 200, ['Content-Type' => 'text/xml']);
            }
        ]);
    }
}

Contributing

Please see CONTRIBUTING for details.

Security

If you discover any security related issues, please email [email protected] instead of using the issue tracker.

Credits

License

The MIT License (MIT). Please see License File for more information.

Star History Chart