路线 [feeds.main] 未使用 spatie/laravel-feed 包定义

Route [feeds.main] not defined with spatie/laravel-feed package

我使用 Laravel 8 和 spatie/laravel-feed 4.0 包,代码如下:

routes/web.php

Route::feeds();

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' => 'en-US',

            /*
             * 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' => '',

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

app/Models/Blog/Post.php

class Post extends Model implements Feedable
{
    // ...

    public function toFeedItem(): FeedItem
    {
        return FeedItem::create([
            'id' => $this->id,
            'title' => $this->title,
            'summary' => $this->summary,
            'updated' => $this->updated_at,
            'link' => $this->link,
            'author' => $this->user->name,
        ]);
    }

    public static function getFeedItems()
    {
        return Post::orderBy('publish_date', 'desc')
            ->limit(10)
            ->get();
    }
}

resources/views/layout.blade.php

<!DOCTYPE html>
<html lang="@yield('lang')">
<head>
    <!-- ... -->

    @include('feed::links')

</head>
<body>
    <!-- ... -->

我尝试运行这些命令:

php artisan route:clear
php artisan optimize

...但注意事项已更改。

我收到此错误消息:

Route [feeds.main] not defined. (View: /var/www/html/vendor/spatie/laravel-feed/resources/views/links.blade.php)

知道我错过了什么吗?

如果您正在寻找即时解决方案,那么您可以删除

Route::feeds();

并添加如下路线

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

并确保 url 的名称在配置文件中匹配

<?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\Order', 'getFeedItems'],


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

            'title' => 'My feed',
            'description' => 'The description of the feed.',
            'language' => 'en-US',

            /*
             * 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' => '',

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

和运行遵循命令

php artisan route:clear

php artisan optimize

更新

简单的解决方案是,如果您在配置文件中指定 url,那么它不会在 config/feed.php

中抛出 error.For 示例
 'url' => '/feed',

所以不需要改变任何想法