PHP code example of phl / laravel-respond-to

1. Go to this page and download the library: Download phl/laravel-respond-to 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/ */

    

phl / laravel-respond-to example snippets




use PHL\LaravelRespondTo\Respond;

class MyController
{
    public function index()
    {
        return Respond::to('html')->withView('hello', ['name' => 'John Doe'])
            ->to('json')->with(['key' => 'value'])
            ->to('txt')->with('some text')
            ->to('xml')->with(function () {})
            ->to('rss')->with(new \Illuminate\Http\Response())
            ->to('css')->with(new ResponsableClass);
    }
}

use PHL\LaravelRespondTo\Respond;

Respond::withNewRespond()
    ->to('json')->with(['key' => 'value'])
    ->default('json');

$formats = [
    'html' => ['text/html', 'application/xhtml+xml'],
    'txt' => ['text/plain'],
    'js' => ['application/javascript', 'application/x-javascript', 'text/javascript'],
    'css' => ['text/css'],
    'json' => ['application/json', 'application/x-json'],
    'jsonld' => ['application/ld+json'],
    'xml' => ['text/xml', 'application/xml', 'application/x-xml'],
    'rdf' => ['application/rdf+xml'],
    'atom' => ['application/atom+xml'],
    'rss' => ['application/rss+xml'],
    'form' => ['application/x-www-form-urlencoded'],
];

request()->setFormat('csv', 'text/csv');

use PHL\LaravelRespondTo\Respond;

Respond::withNewRespond()
    ->to('html')->with('some html')
    ->to('json')->with(['key' => 'value'])
    ->to('xml')->withView('welcome_xml');