【问题标题】:Slim Framework -> Create XML OutputSlim 框架 -> 创建 XML 输出
【发布时间】:2021-06-23 15:33:50
【问题描述】:

如何为 RSS 流创建输出真实 XML 内容的视图。我正在使用 SLIM、TWIG 与 Paris 和 Idiorm 结合进行模板。

类似:

$app -> get('/rss/', function() use ($app) {
$articles = Model::factory('Article') -> order_by_desc('timestamp') -> find_many();
return $app -> render('rss.xml', array('articles' => $articles));
});

使用此模板 layout.xml:

<?xml version="1.0" encoding="UTF-8"?>
{% block content %} {% endblock %}

还有这个用于路由 RSS 的特殊模板:

{% extends 'layout.xml' %}
{% block content %}     
<blog_content>
{% for article in articles %}
<article>
<article_id>{{ article.id }}</article_id>
<article_headline>{{ article.title }}</article_headline>
<article_author>{{ article.author }}</article_author>
<article_timestamp>{{ article.timestamp }}</article_timestamp>
<article_summary>{{ article.summary }}</article_summary>
<article_link>http://slim.phaziz.com/article/{{ article.id }}/</article_link>
</article>
{% endfor %}
</blog_content>
{% endblock %}

将显示为 HTML 文档,其中包含作为正文文本的模板... 标头始终作为 xHTML 而不是 XML 发送

???

感谢您的帮助!

【问题讨论】:

  • 因为没有将标头设置为 XML 的内容类型。这slimframework.com/read/… 有帮助吗?
  • 不,它没有:-(。我认为这是我用作模板机的 Twig 的问题。当我在 Browseroutput 中检查源代码时,XML 会在 HTML-Body 中呈现...

标签: php xml twig slim


【解决方案1】:

更新:此答案不再适用于 Slim 3。


您必须覆盖/rss/ 路由中对 text/xml 的响应的Content-Type HTTP 标头:

$app -> get('/rss/', function() use ($app) {
  $articles = Model::factory('Article') -> order_by_desc('timestamp') -> find_many();
  
  $app->response->headers->set('Content-Type', 'text/xml')
  
  return $app -> render('rss.xml', array('articles' => $articles));
});

http://docs.slimframework.com/response/headers/

编辑:如果您生成的 XML 是 100% RSS,请改用 application/rss+xml 内容类型。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-04-04
    • 1970-01-01
    • 1970-01-01
    • 2018-06-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多