|
| 1 | +--- |
| 2 | +title: Content Negotiable Responses for Laravel |
| 3 | +description: This package makes it easy to aautomaticallly negotiate content based on the client's Accept header. |
| 4 | +type: library |
| 5 | +platform: Laravel |
| 6 | +github: https://github.com/kduma-OSS/LV-content-negotiable-responses |
| 7 | +--- |
| 8 | + |
| 9 | +::u-button-group |
| 10 | +:u-button[GitHub Repository]{icon="bxl:github" href="https://github.com/kduma-OSS/LV-content-negotiable-responses" blank} |
| 11 | + |
| 12 | +:u-button[Releases]{icon="material-symbols:cloud-download" href="https://github.com/kduma-OSS/LV-content-negotiable-responses/releases/latest/" blank} |
| 13 | + |
| 14 | +:u-button[Packagist]{icon="simple-icons:packagist" href="https://packagist.org/packages/kduma/content-negotiable-responses" blank} |
| 15 | +:: |
| 16 | + |
| 17 | +## Installation |
| 18 | + |
| 19 | +```bash |
| 20 | +composer require kduma/content-negotiable-responses |
| 21 | +``` |
| 22 | + |
| 23 | +## Content Formats (for `ArrayResponse` and `BasicViewResponse`) |
| 24 | + |
| 25 | +Currently supported formats are: |
| 26 | +- `text/plain` (disabled by default, to enable it in your custom class implement `TextResponseInterface`) - resulting response will be output of built-in PHP function `print_r` |
| 27 | +- `application/json` - resulting response will be output of built-in PHP function `json_encode` |
| 28 | +- `application/yaml` - resulting response will be generated using [symfony/yaml](https://packagist.org/packages/symfony/yaml) package |
| 29 | +- `application/xml` - resulting response will be generated using [spatie/array-to-xml](https://packagist.org/packages/spatie/array-to-xml) package |
| 30 | +- `application/msgpack` - resulting response will be generated using [rybakit/msgpack](https://packagist.org/packages/rybakit/msgpack) package |
| 31 | +- `text/html` (only for `BasicViewResponse`) - resulting response will be generated using view provided to constructor with passed data array |
| 32 | + |
| 33 | +## Usage |
| 34 | + |
| 35 | +### Basic Array Usage (for API responses) |
| 36 | + |
| 37 | +```php |
| 38 | +Route::get('/test', function () { |
| 39 | + return new \KDuma\ContentNegotiableResponses\ArrayResponse([ |
| 40 | + 'success' => true, |
| 41 | + 'timestamp' => time(), |
| 42 | + ]); |
| 43 | +}); |
| 44 | +``` |
| 45 | +As the result, response will be formatted accordingly to acceptable formats passed in `Accept` HTTP header or as JSON if not specified. |
| 46 | + |
| 47 | +### Basic View Usage (for Web and API responses) |
| 48 | + |
| 49 | +```php |
| 50 | +Route::get('/', function () { |
| 51 | + return new \KDuma\ContentNegotiableResponses\BasicViewResponse('welcome', [ |
| 52 | + 'success' => true, |
| 53 | + 'timestamp' => time(), |
| 54 | + ]); |
| 55 | +}); |
| 56 | +``` |
| 57 | + |
| 58 | +### Customized Usage |
| 59 | + |
| 60 | +```php |
| 61 | +namespace App\Http\Responses; |
| 62 | + |
| 63 | +use KDuma\ContentNegotiableResponses\BaseViewResponse; |
| 64 | +use Illuminate\Database\Eloquent\Collection; |
| 65 | +use Illuminate\Http\Request; |
| 66 | + |
| 67 | +class UsersListResponse extends BaseViewResponse |
| 68 | +{ |
| 69 | + public $users; |
| 70 | + |
| 71 | + public function __construct(Collection $users) |
| 72 | + { |
| 73 | + $this->users = $users; |
| 74 | + } |
| 75 | + |
| 76 | + protected function generateView(Request $request) |
| 77 | + { |
| 78 | + return $this->view('users.list'); |
| 79 | + } |
| 80 | +} |
| 81 | + |
| 82 | +Route::get('/users', function () { |
| 83 | + return new \App\Http\Responses\UsersListResponse(\App\User::all()); |
| 84 | +}); |
| 85 | +``` |
| 86 | + |
| 87 | +As the result, when opening in web browser, there will be rendered `users.list` view with passed all public properties to it. |
| 88 | +In non HTML clients (specyfing other `Accept` headers) will get serialized public properties (in any supported format), for example: |
| 89 | + |
| 90 | +```json |
| 91 | +{ |
| 92 | + "users": [ |
| 93 | + { |
| 94 | + "name": "user 1", |
| 95 | + "email": "user1@localhost" |
| 96 | + }, |
| 97 | + { |
| 98 | + "name": "user 2", |
| 99 | + "email": "user2@localhost" |
| 100 | + }, |
| 101 | + { |
| 102 | + "name": "user 3", |
| 103 | + "email": "user3@localhost" |
| 104 | + } |
| 105 | + ] |
| 106 | +} |
| 107 | +``` |
| 108 | + |
| 109 | +If you want to customize serialized array, you need to override `getDataArray` method: |
| 110 | + |
| 111 | +```php |
| 112 | +/** |
| 113 | + * @return array |
| 114 | + */ |
| 115 | +protected function getDataArray() { |
| 116 | + return [ |
| 117 | + 'my_super_users_collection' => $this->users->toArray(), |
| 118 | + 'hello' => true |
| 119 | + ]; |
| 120 | +} |
| 121 | +``` |
| 122 | + |
| 123 | +Then the result will be: |
| 124 | + |
| 125 | +```json |
| 126 | +{ |
| 127 | + "my_super_users_collection": [ |
| 128 | + { |
| 129 | + "name": "user 1", |
| 130 | + "email": "user1@localhost" |
| 131 | + }, |
| 132 | + { |
| 133 | + "name": "user 2", |
| 134 | + "email": "user2@localhost" |
| 135 | + }, |
| 136 | + { |
| 137 | + "name": "user 3", |
| 138 | + "email": "user3@localhost" |
| 139 | + } |
| 140 | + ], |
| 141 | + "hello": true |
| 142 | +} |
| 143 | +``` |
0 commit comments