Перейти к основному содержимому
Версия: 1.x

← Предыдущая · Назад к README · Следующая →

RSS Atom

Создание файлов​

php artisan make:feed Rss

Заполнение фида​

В сгенерированном классе замените extends Feed на extends RssFeedPreset:

<?php

declare(strict_types=1);

namespace App\Feeds;

use App\Models\News;
use DragonCode\LaravelFeed\Feeds\Items\FeedItem;
use DragonCode\LaravelFeed\Presets\RssFeedPreset;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Model;

class RssFeed extends RssFeedPreset
{
public function builder(): Builder
{
return News::query()->take(2);
}

public function item(Model $model): FeedItem
{
return parent::item($model)
->guid($model->id) // By default, $model->getKey()
->title($model->title)
->description($model->content)
->category($model->category)
->url($model->url)
->publishedAt($model->updated_at) // By default, $model->created_at ?? Carbon::now()
->additional(['foo' => 'bar']); // By default, []
}

public function filename(): string
{
return 'rss.xml';
}
}

Активация​

Проверьте сгенерированный файл операции или миграции, затем выполните подходящую консольную команду:

# For Laravel Deploy Operations
php artisan operations

# For Laravel Migrations
php artisan migrate

Генерация фида​

Сгенерируйте фид следующей консольной командой:

php artisan feed:generate

Результат​

<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:atom="http://www.w3.org/2005/Atom">
<channel>

<item>
<title>Some 1</title>
<link>https://example.com/news/some-1</link>
<guid>1</guid>
<description><![CDATA[Some content 1]]></description>
<category>Some category 1</category>
<pubDate>Wed, 03 Sep 2025 22:35:34 +0000</pubDate>
<foo>bar</foo>
</item>
<item>
<title>Some 2</title>
<link>https://example.com/news/some-2</link>
<guid>2</guid>
<description><![CDATA[Some content 2]]></description>
<category>Some category 2</category>
<pubDate>Thu, 04 Sep 2025 02:44:27 +0000</pubDate>
<foo>bar</foo>
</item>

</channel>
</rss>

Смотрите также​