|
1 | 1 | # laravel-xml-middleware
|
2 | 2 | A Laravel Middleware to accept XML requests
|
| 3 | + |
| 4 | +## Configuration |
| 5 | +### Install Through Composer |
| 6 | +``` |
| 7 | +composer require tucker-eric/laravel-xml-middleware |
| 8 | +``` |
| 9 | + |
| 10 | +### Register The Service Provider |
| 11 | +In `config/app.php` add the service provider to the providers array: |
| 12 | + |
| 13 | +```php |
| 14 | + 'providers' => [ |
| 15 | + //Other Service Providers |
| 16 | + XmlMiddleware\XmlRequestServiceProvider::class, |
| 17 | + ]; |
| 18 | +``` |
| 19 | + |
| 20 | +### Register the middleware |
| 21 | +In `app/Http/Kernel.php` |
| 22 | + |
| 23 | +```php |
| 24 | + protected $routeMiddleware = [ |
| 25 | + /// Other Middleware |
| 26 | + 'xml' => XmlRequestMiddleware::class, |
| 27 | + ]; |
| 28 | +``` |
| 29 | + |
| 30 | +### Applying the middleware to routes |
| 31 | +Add the middleware to your as desired |
| 32 | + |
| 33 | +#### Controller Middleware |
| 34 | +```php |
| 35 | +class MyController extends Controller |
| 36 | +{ |
| 37 | + public function __construct() |
| 38 | + { |
| 39 | + $this->middleware('xml'); |
| 40 | + } |
| 41 | +} |
| 42 | +``` |
| 43 | + |
| 44 | +#### Route Middleware |
| 45 | +```php |
| 46 | + Route::group(['middleware' => 'xml'], function() { |
| 47 | + Route::post('my-api-endpoint', 'MyOtherController@store'); |
| 48 | + }); |
| 49 | +``` |
| 50 | + |
| 51 | +### Accessing XML Input With Middleware |
| 52 | +If you are using the middleware it will automatically inject the xml into the request as an array and you you can access the xml data in your controller with the `$request->all()`: |
| 53 | + |
| 54 | +```php |
| 55 | + |
| 56 | +use Illuminate\Http\Request; |
| 57 | +use App\Http\Controllers\Controller; |
| 58 | + |
| 59 | +class MyController extends Controller |
| 60 | +{ |
| 61 | + public function __construct() |
| 62 | + { |
| 63 | + $this->middleware('xml'); |
| 64 | + } |
| 65 | + |
| 66 | + public function store(Request $request) |
| 67 | + { |
| 68 | + $request->all(); |
| 69 | + } |
| 70 | +} |
| 71 | +``` |
| 72 | +### Accessing XML Input |
| 73 | +To access the xml input without the middleware use the `xml()` method on the `Request`: |
| 74 | + |
| 75 | +```php |
| 76 | +use Illuminate\Http\Request; |
| 77 | +use App\Http\Controllers\Controller; |
| 78 | + |
| 79 | +Class MyOtherController extends Controller |
| 80 | +{ |
| 81 | + public function store(Request $request) |
| 82 | + { |
| 83 | + $xml = $request->xml(); |
| 84 | + } |
| 85 | +} |
| 86 | +``` |
| 87 | + |
| 88 | +To access the xml request as an object pass `false` to the `xml()` method: |
| 89 | + |
| 90 | +```php |
| 91 | +use Illuminate\Http\Request; |
| 92 | +use App\Http\Controllers\Controller; |
| 93 | + |
| 94 | +Class MyOtherController extends Controller |
| 95 | +{ |
| 96 | + public function store(Request $request) |
| 97 | + { |
| 98 | + $xml = $request->xml(false); |
| 99 | + } |
| 100 | +} |
| 101 | +``` |
0 commit comments