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.5.0(2mo ago)1041.3M↓30.5%22[6 issues](https://github.com/mtownsend5512/response-xml/issues)3MITPHPPHP ~7.0|~8.0CI failing

Since Oct 22Pushed 2mo 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 2d ago

READMEChangelog (10)Dependencies (14)Versions (15)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

63

—

FairBetter than 99% of packages

Maintenance84

Actively maintained with recent releases

Popularity55

Moderate usage in the ecosystem

Community25

Small or concentrated contributor base

Maturity74

Established project with proven stability

 Bus Factor1

Top contributor holds 61.9% 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 ~210 days

Recently: every ~321 days

Total

14

Last Release

77d 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 (26 commits)")[![laravel-shift](https://avatars.githubusercontent.com/u/15991828?v=4)](https://github.com/laravel-shift "laravel-shift (4 commits)")[![YerlenZhubangaliyev](https://avatars.githubusercontent.com/u/1582996?v=4)](https://github.com/YerlenZhubangaliyev "YerlenZhubangaliyev (4 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)")[![rodelias-frete](https://avatars.githubusercontent.com/u/281798633?v=4)](https://github.com/rodelias-frete "rodelias-frete (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

[psalm/plugin-laravel

Psalm plugin for Laravel

3355.3M346](/packages/psalm-plugin-laravel)[laravel/mcp

Rapidly build MCP servers for your Laravel applications.

77022.3M150](/packages/laravel-mcp)[api-platform/laravel

API Platform support for Laravel

58170.8k14](/packages/api-platform-laravel)[roots/acorn

Framework for Roots WordPress projects built with Laravel components.

9762.4M131](/packages/roots-acorn)[spatie/laravel-responsecache

Speed up a Laravel application by caching the entire response

2.8k9.0M69](/packages/spatie-laravel-responsecache)[defstudio/telegraph

A laravel facade to interact with Telegram Bots

816333.3k3](/packages/defstudio-telegraph)

PHPackages © 2026

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