【问题标题】:How to set up spatie/laravel-feed content type to xml?如何将 spatie/laravel-feed 内容类型设置为 xml?
【发布时间】:2021-06-29 08:10:56
【问题描述】:

我将 Laravel v8 与 spatie/laravel-feed v4 一起使用,代码如下:

routes/web.php

Route::get('feed', '\App\Models\Blog\Post@getFeedItems')->name("feeds.main");

config/feed.php

<?php

return [
    'feeds' => [
        'main' => [
            /*
             * Here you can specify which class and method will return
             * the items that should appear in the feed. For example:
             * [App\Model::class, 'getAllFeedItems']
             *
             * You can also pass an argument to that method.  Note that their key must be the name of the parameter:             *
             * [App\Model::class, 'getAllFeedItems', 'parameterName' => 'argument']
             */
            'items' => ['App\Models\Blog\Post', 'getFeedItems'],

            /*
             * The feed will be available on this url.
             */
            'url' => '/feed',

            'title' => 'News',
            'description' => 'The description of the feed.',
            'language' => 'hu-HU',

            /*
             * The image to display for the feed.  For Atom feeds, this is displayed as
             * a banner/logo; for RSS and JSON feeds, it's displayed as an icon.
             * An empty value omits the image attribute from the feed.
             */
            'image' => '',

            /*
             * The format of the feed.  Acceptable values are 'rss', 'atom', or 'json'.
             */
            'format' => 'atom',

            /*
             * The view that will render the feed.
             */
            'view' => 'feed::atom',

            /*
             * The mime type to be used in the <link> tag.  Set to an empty string to automatically
             * determine the correct value.
             */
            'type' => 'application/atom+xml',

            /*
             * The content type for the feed response.  Set to an empty string to automatically
             * determine the correct value.
             */
            'contentType' => '',
        ],
    ],
];

网页HTML代码中的结果:

<link rel="alternate" type="application/atom+xml" href="http://mydomain.test/feed" title="News">

但如果我在浏览器中打开链接,结果会包含 JSON 代码。

如何正确设置/强制application/atom+xml 内容类型?

【问题讨论】:

  • 更新'contentType' =&gt; 'application/xml'
  • @RamanathMandal 感谢建议,但无济于事
  • @netdjw.i 已检查其返回的 xml。尝试清除视图或更改浏览器
  • @JohnLobo 我试过了,但没有帮助,我仍然得到 JSON 格式
  • @netdjw.strange 我已经在 laravel 8 中尝试过它的工作

标签: laravel rss laravel-8 content-type


【解决方案1】:

错误在路线中

Route::get('feed', '\App\Models\Blog\Post@getFeedItems')->name("feeds.main");

应该是这样的

Route::get('/feed',FeedController::class)->name("feeds.main");

Route::feeds();

【讨论】:

  • 我尝试了这两个建议,但都没有奏效。
  • @netdjw。它有效。尝试清除路由缓存 php artisan route:clear ,php artisan view:clear 和 php artisan config:clear
  • 删除 Route::get('feed', '\App\Models\Blog\Post@getFeedItems')->name("feeds.main");这条路线
猜你喜欢
  • 2019-03-21
  • 2018-05-22
  • 2012-05-18
  • 2016-09-28
  • 2011-08-12
  • 2011-07-26
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多