Package Data | |
---|---|
Maintainer Username: | bmatovu |
Maintainer Contact: | mtvbrianking@gmail.com (Brian Matovu) |
Package Create Date: | 2018-07-01 |
Package Last Update: | 2024-04-16 |
Home Page: | |
Language: | PHP |
License: | MIT |
Last Refreshed: | 2024-11-14 15:16:35 |
Package Statistics | |
---|---|
Total Downloads: | 160,747 |
Monthly Downloads: | 6,172 |
Daily Downloads: | 245 |
Total Stars: | 85 |
Total Watchers: | 2 |
Total Forks: | 9 |
Total Open Issues: | 6 |
This package comes with the much desired xml support for you Laravel project.
Supports: Laravel versions v5.3 and above
composer require bmatovu/laravel-xml
Get the request content (body).
$request->xml();
* Returns Bmatovu\LaravelXml\Support\XMLElement
object.
Determine if the request content type is XML.
$request->sentXml();
Determine if the current request is accepting XML.
$request->wantsXml();
Validate XML content
$isValid = Xml::is_valid($request->xml());
if (! $isValid) {
return response()->xml(['message' => 'The given data was malformed.'], 400);
}
Validation - Against XML Schema Definition
$errors = Xml::validate($request->xml(), 'path_to/sample.xsd');
if ($errors) {
return response()->xml([
'message' => 'The given data was invalid.',
'errors' => $errors,
], 422);
}
Route::get('/users/{user}', function (Request $request, int $userId) {
$user = User::findOrFail($userId);
return response()->xml($user);
});
<?xml version="1.0" encoding="UTF-8"?>
<document>
<id>1</id>
<name>jdoe</name>
<email>jdoe@example.com</email>
</document>
Route::get('/users/{user}', function (Request $request, int $userId) {
$user = User::findOrFail($userId);
return response()->xml(['user' => $user->toArray()]);
});
<?xml version="1.0" encoding="UTF-8"?>
<document>
<user>
<id>1</id>
<name>jdoe</name>
<email>jdoe@example.com</email>
</user>
</document>
Route::get('/users/{user}', function (Request $request, int $userId) {
$user = User::findOrFail($userId);
return response()->xml($user, 200, [], ['root' => 'user']);
});
<?xml version="1.0" encoding="UTF-8"?>
<user>
<id>1</id>
<name>jdoe</name>
<email>jdoe@example.com</email>
</user>
Route::get('/users', function () {
$users = User::get();
return response()->xml(['users' => $users->toArray()]);
});
<?xml version="1.0" encoding="UTF-8"?>
<document>
<users>
<id>1</id>
<name>John Doe</name>
<email>jdoe@example.com</email>
</users>
<users>
<id>2</id>
<name>Gary Plant</name>
<email>gplant@example.com</email>
</users>
</document>
And will automatically set the content type to xml
Content-Type → text/xml; charset=UTF-8
First register the middleware in app\Http\Kernel.php
protected $routeMiddleware = [
// ...
'xml' => \Bmatovu\LaravelXml\Http\Middleware\RequireXml::class,
];
Then use the middleware on your routes, or in the controllers.
Route::post('/users', function (Request, $request) {
// do something...
})->middleware('xml');
This middleware only checks the Content-Type
by defaul;
[415
- Unsupported Media Type]
<?xml version="1.0" encoding="UTF-8"?>
<document>
<message>Only accepting content of type XML.</message>
</document>
To check is the passed content is valid XML, pass a bool to the middleware
Route::post('/users', function (Request, $request) {
// do something...
})->middleware('xml:1');
[400
- Bad Request]
<?xml version="1.0" encoding="UTF-8"?>
<document>
<message>The given data was malformed.</message>
</document>
Encode: Array to Xml
Xml::encode(['key' => 'value']);
Or
xml_encode(['key' => 'value']);
Decode: Xml to Array
Xml::decode('<?xml version="1.0" encoding="UTF-8"?><document><key>value</key></document>');
Or
xml_decode('<?xml version="1.0" encoding="UTF-8"?><document><key>value</key></document>');
Under the hood, I'm using;
Spatie's array to XML convernsion
Hakre's XML to JSON conversion
If you've stumbled across a bug, please help us by leaving as much information about the bug as possible, e.g.
This will help us to fix the bug as quickly as possible, and if you do wish to fix it yourself; feel free to fork the package on GitHub and submit a pull request!