1

I cant deal with such simple (I think) thing like output records from database as XML. I have eloquent model named Customer and I want to send to output all customers from db so I've tried to install response macro ( http://www.laravel-tricks.com/tricks/responsexml-macro ) and I call it like this:

public function showCustomers()
{
   $customers = Customer::all()->toArray();
   return Response::xml($customers);
}

but then I got error "SimpleXMLElement::addChild(): unterminated entity reference M" . I tried also other solution which used SimpleXMLELement also so the result was the same.

Lukas
  • 621
  • 2
  • 12
  • 29
  • 1
    Read the comments on [laravel-tricks](http://www.laravel-tricks.com/tricks/responsexml-macro#comment-1453496585). There's a modified version that supports numeric arrays (which you need) – lukasgeiter Dec 28 '14 at 13:43
  • Almost complete solution. I've also changed a little code that is described there : http://stackoverflow.com/a/17028414/2487793 – Lukas Dec 28 '14 at 13:55
  • 2
    I suggest you answer your own question and document what you did for future visitors of this post. – lukasgeiter Dec 28 '14 at 13:57

3 Answers3

0

Try this package:

https://github.com/mtownsend5512/response-xml

Then it's as easy as

$customers = Customer::all();
return response()->xml($customers);
Mark
  • 1,255
  • 1
  • 13
  • 25
0

https://github.com/dwightwatson/sitemap - very useful utility, works with Laravel 4.*, 5.*.

Install:

composer require watson/sitemap

Add the service provider to your config/app.php file.

Watson\Sitemap\SitemapServiceProvider::class

Add the alias to the facade, also in config/app.php.

'Sitemap' => Watson\Sitemap\Facades\Sitemap::class

Usage example:

namespace App\Http\Controllers;

use App\Models\Categories;
use App\Models\Essays;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Str;
use Watson\Sitemap\Sitemap;

/**
 * Class SitemapController, generates and shows xml sitemap
 *
 * @package App\Http\Controllers
 */
class SitemapController
{
    public function index() {
        // init sitemap obj
        $sitemap = new Sitemap(
            Cache::store('file'),
            new Request()
        );

        $essayModel = new Essays();
        $categoryModel = new Categories();

        $categoriesArr = $categoryModel->getAllPublishedCategories();

        // add to sitemap all the categories
        foreach ($categoriesArr as $categoryRes) {
            $sitemap->addTag(
                url(
                    "/" . config('custom.urlBases.essaysCategory')
                    . "/" . Str::slug($categoryRes->name, "-") . "-{$categoryRes->id}"
                ),
                $categoryRes->cat_publication_time,
                'daily',
                '0.5'
            );
        }

        $essaysArr = $essayModel->getAllEssays();

        // add to sitemap all the essays
        foreach ($essaysArr as $essay) {
            $sitemap->addTag(
                url(config('custom.urlBases.essayPage') . '/' . $essay->url . '-' . $essay->id),
                $essay->publication_time,
                'daily',
                '0.5'
            );
        }

        return $sitemap->render();
    }
}

Returns content like next:

<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
<url>
    <loc>page-url-here</loc>
    <lastmod>2017-02-08T19:52:37+00:00</lastmod>
    <changefreq>daily</changefreq>
    <priority>0.5</priority>
</url>
Vaha
  • 2,179
  • 2
  • 17
  • 29
-1
<?php
// macros.php
Response::macro('xml', function(array $vars, $status = 200, array $header = [], $xml = null)
{
    if (is_null($xml)) {
        $xml = new SimpleXMLElement('<?xml version="1.0" encoding="UTF-8"?><response/>');
    }
    foreach ($vars as $key => $value) {
        if (is_array($value)) {
            Response::xml($value, $status, $header, $xml->addChild($key));
        } else {
            $xml->addChild($key, $value);
        }
    }
    if (empty($header)) {
        $header['Content-Type'] = 'application/xml';
    }
    return Response::make($xml->asXML(), $status, $header);
});
?>

<?php
// app/start/global.php
// add require macros.php
require app_path() . '/macros.php';
?>

<?php
// How to use
// routes.php
Route::get('api.{ext}', function()
{
    $data = ['status' => 'OK'];
    $ext = File::extension(Request::url());
    return Response::$ext($data);
})->where('ext', 'xml|json');
Ley
  • 29
  • 4