Slim Framework & # 8594; Create XML Output

How can I create a view that displays real XML content for an RSS feed. I use SLIM, TWIG for templates in combination with Paris and Idiorm.

Sort of:

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

With this layout.xml template:

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

And this special template for the RSS route:

 {% 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 %} 

Will display as an HTML Doument that contains Templates as Body Text ... The header is always sent as xHTML, not XML

???

Thanx for help!

+4
source share
1 answer

Update: This answer is no longer applicable in Slim version 3.


You need to rewrite the HTTP header of the Content-Type response to text/xml in your /rss/ route:

 $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/

Edit: in case your generated XML is 100% RSS, use application/rss+xml .

+5
source

Source: https://habr.com/ru/post/1399400/


All Articles