PHPackages                             mtownsend/response-xml - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [API Development](/categories/api)
4. /
5. mtownsend/response-xml

ActiveLibrary[API Development](/categories/api)

mtownsend/response-xml
======================

The missing XML support for Laravel's Response class.

2.4.0(1y ago)1041.2M—0.7%22[6 issues](https://github.com/mtownsend5512/response-xml/issues)[1 PRs](https://github.com/mtownsend5512/response-xml/pulls)3MITPHPPHP ~7.0|~8.0CI failing

Since Oct 22Pushed 1y ago2 watchersCompare

[ Source](https://github.com/mtownsend5512/response-xml)[ Packagist](https://packagist.org/packages/mtownsend/response-xml)[ RSS](/packages/mtownsend-response-xml/feed)WikiDiscussions master Synced 1mo ago

READMEChangelog (10)Dependencies (7)Versions (14)Used By (3)

The missing XML support for Laravel's Response class.

This package is designed to work with the [Laravel](https://laravel.com) framework.

Installation
------------

[](#installation)

Install via composer:

```
composer require mtownsend/response-xml

```

### Registering the service provider

[](#registering-the-service-provider)

For Laravel 5.4 and lower, add the following line to your `config/app.php`:

```
/*
 * Package Service Providers...
 */
Mtownsend\ResponseXml\Providers\ResponseXmlServiceProvider::class,
```

For Laravel 5.5 and greater, the package will auto register the provider for you.

### Using Lumen

[](#using-lumen)

To register the service provider, add the following line to `app/bootstrap/app.php`:

```
$app->register(Mtownsend\ResponseXml\Providers\ResponseXmlServiceProvider::class);
```

Quick start
-----------

[](#quick-start)

### Respond with xml

[](#respond-with-xml)

```
$data = [
    'status' => 'success',
    'data' => [
        'first_name' => 'John',
        'last_name' => 'Smith',
    ]
];
return response()->xml($data);

// Returns:

    success

        John
        Smith

```

### Respond with xml from a collection or eloquent query

[](#respond-with-xml-from-a-collection-or-eloquent-query)

You may also pass a collection to be transformed into xml.

```
return response()->xml(User::all());
```

### Respond with existing xml

[](#respond-with-existing-xml)

If you already have xml, you can pass it to the `xml` method to respond.

```
$xml =  'success',
    'data' => [
        'first_name' => 'John',
        'last_name' => 'Smith',
    ]
];
return response()->preferredFormat($data);
```

Methods and arguments
---------------------

[](#methods-and-arguments)

**Response method**

`->xml($xml, $status = 200, array $headers = [], $xmlRoot = 'response', $encoding = null)`

The `$xml` argument is the data you want to be transformed into xml (may also be a premade xml string).

`$status` is the http code your response will send.

`$headers` is an array of key/values of http headers your response will return. A default header of `Content-Type: application/xml` is automatically applied, but can be overwritten.

`$xmlRoot` will change the root xml element. Default is `response`.

`$encoding` is a string of xml charset encoding declaration. Default is `null`.

**Response method**

`->preferredFormat($data, $status = 200, array $headers = [], $xmlRoot = 'response', $encoding = null)`

See `->xml()` method arguments.

The only difference between this method and `->xml()` is `$data` can potentially be transformed to json and `$xmlRoot` will be ignored if the response is json.

Purpose
-------

[](#purpose)

Have you ever found yourself wishing Laravel offered the same exemplary support for returning XML responses as it does for JSON? Imagine you are creating an api platform and want to be inclusive of other apps that would prefer to make XML requests to your application - sometimes the reasons are more than preferential. Wouldn't it be a dream if you could return XML as simply as writing `return response()->xml($data);` and it just worked? **Now you can!**

This package achieves one critical goal: respond with XML as easily as you can with JSON in your Laravel application.

Other packages you may be interested in
---------------------------------------

[](#other-packages-you-may-be-interested-in)

- [mtownsend/collection-xml](https://github.com/mtownsend5512/collection-xml)
- [mtownsend/request-xml](https://github.com/mtownsend5512/request-xml)
- [mtownsend/xml-to-array](https://github.com/mtownsend5512/xml-to-array)

Credits
-------

[](#credits)

- Mark Townsend
- [Spatie](https://spatie.be/)
- [All Contributors](../../contributors)

Testing
-------

[](#testing)

You can run the tests with:

```
./vendor/bin/phpunit
```

License
-------

[](#license)

The MIT License (MIT). Please see [License File](LICENSE.md) for more information.

###  Health Score

53

—

FairBetter than 97% of packages

Maintenance42

Moderate activity, may be stable

Popularity55

Moderate usage in the ecosystem

Community25

Small or concentrated contributor base

Maturity74

Established project with proven stability

 Bus Factor1

Top contributor holds 64.1% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~192 days

Recently: every ~274 days

Total

13

Last Release

451d ago

Major Versions

1.3.1 → 2.0.02021-01-11

PHP version history (2 changes)1.0.0PHP ~7.0

2.0.0PHP ~7.0|~8.0

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/812987?v=4)[mtownsend](/maintainers/mtownsend)[@mtownsend](https://github.com/mtownsend)

---

Top Contributors

[![mtownsend5512](https://avatars.githubusercontent.com/u/4945553?v=4)](https://github.com/mtownsend5512 "mtownsend5512 (25 commits)")[![YerlenZhubangaliyev](https://avatars.githubusercontent.com/u/1582996?v=4)](https://github.com/YerlenZhubangaliyev "YerlenZhubangaliyev (4 commits)")[![laravel-shift](https://avatars.githubusercontent.com/u/15991828?v=4)](https://github.com/laravel-shift "laravel-shift (3 commits)")[![michalkortas](https://avatars.githubusercontent.com/u/1836004?v=4)](https://github.com/michalkortas "michalkortas (2 commits)")[![tkrtmy](https://avatars.githubusercontent.com/u/4525749?v=4)](https://github.com/tkrtmy "tkrtmy (2 commits)")[![kudashevs](https://avatars.githubusercontent.com/u/15892462?v=4)](https://github.com/kudashevs "kudashevs (1 commits)")[![thanonoc](https://avatars.githubusercontent.com/u/31168024?v=4)](https://github.com/thanonoc "thanonoc (1 commits)")[![chapeupreto](https://avatars.githubusercontent.com/u/834048?v=4)](https://github.com/chapeupreto "chapeupreto (1 commits)")

---

Tags

apiarray-to-xmllaravellaravel-packageresponsexmlresponseapilaravelxml

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/mtownsend-response-xml/health.svg)

```
[![Health](https://phpackages.com/badges/mtownsend-response-xml/health.svg)](https://phpackages.com/packages/mtownsend-response-xml)
```

###  Alternatives

[essa/api-tool-kit

set of tools to build an api with laravel

52680.5k](/packages/essa-api-tool-kit)[api-platform/laravel

API Platform support for Laravel

59126.4k6](/packages/api-platform-laravel)[dragon-code/laravel-json-response

Automatically always return a response in JSON format

1118.6k1](/packages/dragon-code-laravel-json-response)[vinelab/api-manager

Laravel API Manager Package - beatify and unify your responses with the least effort possible.

392.1k](/packages/vinelab-api-manager)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
