Skip to content
Proyek

Tentang

Framework-agnostic PHP Feed generator for Laravel, Symfony, and any PHP project. Generate RSS and Atom feeds with full caching support and customizable views.

Pengembangan web dan aplikasi

Cara angka ini diukur

Yang dapat Anda lakukan

  • 🚀 Framework Agnostic: Works with Laravel, Symfony, or any PHP project
  • 📡 Multiple Formats: RSS 2.0, Atom 1.0, and JSON Feed 1.1
  • ⚡ Caching: Built-in caching support with framework adapters
  • 🎨 Custom Views: Use your own templates for feed generation

Pertumbuhan bintang terukur

dalam 24 jam
0
dalam 7 hari
0
dalam 15 hari
0
dalam 1 bulan
Pembaruan tertunda

Perubahan bersih pada periode yang tumpang tindih, bukan total harian. Pengukuran yang belum tersedia dibiarkan kosong. Bintang menunjukkan perhatian, bukan kualitas.

README.md

Asli

php-feed package

CI Analyze Style CodeQL Dependabot codecov

A modern, framework-agnostic PHP Feed generator for Laravel, Symfony, and any PHP project. Generate RSS, Atom, and JSON Feed 1.1 documents with caching support and customizable views.

📦 Part of the Feed Family

  • 🐘 php-feed - PHP 8.3+ implementation with Laravel and Symfony support (this package)
  • 📘 npm-feed - TypeScript implementation for Node.js
  • 🔷 go-feed - Go implementation

🔗 Recommended Projects

  • 📅 php-calendar - RFC 5545 calendar files for Laravel, Symfony, and plain PHP
  • 🤖 php-chatbot - AI chat completions for PHP applications
  • 🌍 php-geolocation - Cloudflare visitor country, language, and client headers
  • 🔍 php-seo - Meta tags, structured data, and SEO helpers
  • 🗺️ php-sitemap - XML, HTML, text, and Google News sitemaps
  • 👤 php-vcard - vCard contact files for PHP applications

Features

  • 🚀 Framework Agnostic: Works with Laravel, Symfony, or any PHP project
  • 📡 Multiple Formats: RSS 2.0, Atom 1.0, and JSON Feed 1.1
  • ⚡ Caching: Built-in caching support with framework adapters
  • 🎨 Custom Views: Use your own templates for feed generation
  • 🔧 Dependency Injection: Clean architecture with adapter pattern
  • ✅ 100% Test Coverage: Thoroughly tested with Pest
  • 📋 PSR-12 Compliant: Follows modern PHP standards
  • 🔒 Type Safe: Full PHP 8.3+ type declarations

Requirements

  • PHP 8.3+ (security support until 31 Dec 2027; also tested on 8.4 and 8.5)
  • Composer

Installation

composer require rumenx/php-feed

Usage Examples

Laravel

Basic Usage:

use Rumenx\Feed\FeedFactory;

class FeedController extends Controller
{
    public function feed()
    {
        $feed = FeedFactory::create();
        $feed->setTitle('My Blog Feed');
        $feed->setDescription('Latest posts from my blog');
        $feed->setLink('https://example.com');
        
        $feed->addItem([
            'title' => 'First Post',
            'author' => 'Rumen',
            'link' => 'https://example.com/post/1',
            'pubdate' => now(),
            'description' => 'This is the first post.'
        ]);
        
        // Return XML string directly
        $xml = $feed->render('rss');
        return response($xml, 200, [
            'Content-Type' => 'application/xml'
        ]);
    }
}

Using Laravel Views (Optional):

For more control, you can use the included Blade templates:

use Rumenx\Feed\FeedFactory;

class FeedController extends Controller  
{
    public function feed()
    {
        $feed = FeedFactory::create();
        $feed->setTitle('My Blog Feed');
        $feed->addItem([
            'title' => 'First Post',
            'author' => 'Rumen', 
            'link' => 'https://example.com/post/1',
            'pubdate' => now(),
            'description' => 'This is the first post.'
        ]);
        
        // Get data for your own view template
        $items = $feed->getItems();
        $channel = [
            'title' => $feed->getTitle(),
            'description' => $feed->getDescription(),
            'link' => $feed->getLink()
        ];
        
        return response()->view('feed.rss', compact('items', 'channel'), 200, [
            'Content-Type' => 'application/xml'
        ]);
    }
}

Symfony

Basic Usage:

use Rumenx\Feed\FeedFactory;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Response;

class FeedController extends AbstractController
{
    public function feed(): Response
    {
        $feed = FeedFactory::create();
        $feed->setTitle('My Blog Feed');
        $feed->setDescription('Latest posts from my blog');
        $feed->setLink('https://example.com');
        
        $feed->addItem([
            'title' => 'First Post',
            'author' => 'Rumen',
            'link' => 'https://example.com/post/1',
            'pubdate' => new \DateTime(),
            'description' => 'This is the first post.'
        ]);
        
        // Return XML response
        $xml = $feed->render('atom');
        return new Response($xml, 200, ['Content-Type' => 'application/xml']);
    }
}

Using Symfony Views (Optional):

For more control, you can use Twig templates:

class FeedController extends AbstractController
{
    public function feed(): Response
    {
        $feed = FeedFactory::create();
        $feed->setTitle('My Blog Feed');
        $feed->addItem([
            'title' => 'First Post',
            'author' => 'Rumen',
            'link' => 'https://example.com/post/1',
            'pubdate' => new \DateTime(),
            'description' => 'This is the first post.'
        ]);
        
        // Get data for your own Twig template
        $items = $feed->getItems();
        $channel = [
            'title' => $feed->getTitle(),
            'description' => $feed->getDescription(),
            'link' => $feed->getLink()
        ];
        
        return $this->render('feed/atom.xml.twig', [
            'items' => $items,
            'channel' => $channel
        ], new Response('', 200, ['Content-Type' => 'application/xml']));
    }
}

Plain PHP / Other Frameworks

Simple Usage (Recommended):

require 'vendor/autoload.php';

use Rumenx\Feed\FeedFactory;

// Create feed with simple built-in adapters
$feed = FeedFactory::create();
$feed->setTitle('My Feed');
$feed->setDescription('Feed description');
$feed->setLink('https://example.com');

$feed->addItem([
    'title' => 'Hello World',
    'author' => 'Rumen',
    'link' => 'https://example.com/hello',
    'pubdate' => date('c'),
    'description' => 'Hello world post!'
]);

// Output RSS feed
header('Content-Type: application/xml');
echo $feed->render('rss');

JSON Feed

render('json') writes JSON Feed 1.1 with content type application/feed+json. toJsonFeed() returns the JSON string.

$feed->setFeedUrl('https://example.com/feed.json');
$feed->setAuthor('[email protected] (Editor)');
$feed->addItem([
    'title' => 'First Post',
    'author' => 'Ada Lovelace',
    'link' => 'https://example.com/posts/first',
    'guid' => 'https://example.com/posts/first',
    'pubdate' => date('c'),
    'description' => '<p>This is my first post.</p>',
    'content_text' => 'This is my first post.',
    'category' => ['news'],
]);

header('Content-Type: application/feed+json');
echo $feed->toJsonFeed();

setFeedUrl() sets feed_url. setLink() becomes home_page_url. An item id is guid, id, or link. HTML in description is content_html. Plain text is content_text. Discovery links use Feed::link($url, 'json', 'JSON Feed').

Advanced - Custom Adapters:

You can provide your own implementations for cache, config, response, and view:

use Rumenx\Feed\Feed;

$feed = new Feed([
    'cache' => new MyCacheAdapter(),
    'config' => new MyConfigAdapter(),
    'response' => new MyResponseAdapter(),
    'view' => new MyViewAdapter(),
]);

$feed->setTitle('My Feed');
$feed->addItem([
    'title' => 'Hello',
    'author' => 'Rumen',
    'link' => 'https://example.com/hello',
    'pubdate' => date('c'),
    'description' => 'Hello world!'
]);

// Use render() for framework-specific response
// or render() for XML string in plain PHP
echo $feed->render('rss');

Development

Available Composer Scripts

# Run tests
composer test

# Run tests with coverage
composer coverage

# Generate an HTML coverage report
composer coverage-html

# Run static analysis
composer analyze

# Check code style
composer style

# Fix code style
composer fix

# Run tests, analysis, and style
composer quality

API Reference

Factory Method

// Create feed with simple adapters (recommended for most users)
$feed = FeedFactory::create($config);

Core Methods

// Feed configuration
$feed->setTitle(string $title): self
$feed->setDescription(string $description): self
$feed->setLink(string $link): self
$feed->setDateFormat(string $format): self
$feed->setLanguage(string $language): self

// Item management
$feed->addItem(array $item): self
$feed->addItems(array $items): self

// Rendering
$feed->render(string $format = 'rss'): mixed // Returns framework-specific response or XML string

// Caching
$feed->isCached(string $key): bool
$feed->clearCache(string $key): self

Architecture

This package follows a clean architecture pattern with dependency injection:

  • Feed: Core feed generator class
  • Adapters: Framework-specific implementations
    • FeedCacheInterface: Caching operations
    • FeedConfigInterface: Configuration access
    • FeedResponseInterface: HTTP response handling
    • FeedViewInterface: Template rendering

Support This Project

If you find this project useful, please consider supporting its development:

GitHub Sponsors

Other ways to support:

  • ⭐ Star this repository
  • 🐛 Report bugs and suggest improvements
  • 💻 Contribute code or documentation
  • 💖 Make a donation - See FUNDING.md for details

Contributing

We welcome contributions! Please see our Contributing Guide for details.

Security

If you discover any security-related issues, please check our Security Policy.

License

This package is open-sourced software licensed under the MIT License.

Links

Jelajahi proyek terkait

Memuat…