A Laravel Middleware to accept XML requests
composer require tucker-eric/laravel-xml-middlewareRegister The Service Provider Inconfig/app.php add the service provider to the providers array:
'providers ' => [//Other Service Providers XmlMiddleware \XmlRequestServiceProvider::class, ];Inapp/Http/Kernel.php
protected $ routeMiddleware = [/// Other Middleware 'xml ' => \XmlMiddleware \XmlRequestMiddleware::class, ];Applying the middleware to routes Add the middleware to your route as desired
class MyControllerextends Controller{public function __construct () {$ this ->middleware ('xml ' ); }} Route::group (['middleware ' =>'xml ' ],function () { Route::post ('my-api-endpoint ' ,'MyOtherController@store ' ); }); Route::post ('my-api-endpoint ' ,'MyOtherController@store ' )->middleware ('xml ' ); Accessing XML Input With Middleware 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():
use Illuminate \Http \Request ;use App \Http \Controllers \Controller ;class MyControllerextends Controller{public function __construct () {$ this ->middleware ('xml ' ); }public function store (Request $ request ) {$ request ->all (); }}To access the xml input without the middleware use thexml() method on theRequest:
use Illuminate \Http \Request ;use App \Http \Controllers \Controller ;Class MyOtherControllerextends Controller{public function store (Request $ request ) {$ xml =$ request ->xml (); }}To access the xml request as an object passfalse to thexml() method:
use Illuminate \Http \Request ;use App \Http \Controllers \Controller ;Class MyOtherControllerextends Controller{public function store (Request $ request ) {$ xml =$ request ->xml (false ); }}