Laravel如何将db记录输出为XML?

时间:2014-12-28 13:26:57

标签: php xml api laravel

我无法处理像数据库中的输出记录那样简单(我认为)这样的事情。我有一个名为Customer的雄辩模型,我想从db输出所有客户,所以我试图安装响应宏(http://www.laravel-tricks.com/tricks/responsexml-macro),我这样称呼它:

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

然后我收到错误" SimpleXMLElement :: addChild():未终止的实体引用M" 。我也尝试了其他使用SimpleXMLELement的解决方案,结果也一样。

3 个答案:

答案 0 :(得分:0)

尝试此套餐:

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

那么就像

$customers = Customer::all();
return response()->xml($customers);

答案 1 :(得分:0)

https://github.com/dwightwatson/sitemap-非常有用的实用程序,可与 Laravel 4。*,5。* 一起使用。

安装:

manage.py compilemessages

将服务提供商添加到您的config / app.php文件中。

composer require watson/sitemap

将别名添加到外观中,同样在config / app.php中。

Watson\Sitemap\SitemapServiceProvider::class

用法示例:

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

像下面一样返回内容:

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();
    }
}

答案 2 :(得分:-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');