Customization | laravel-feed-rust | Spatie       

 SPATIE  

laravel-feed-rust
=================

spatie.be/open-source

  [Docs](https://spatie.be/docs)  [Laravel-feed-rust](https://spatie.be/docs/laravel-feed-rust/v1)  Customization

 Version   v1      

 Other versions for crawler [v1](https://spatie.be/docs/laravel-feed-rust/v1) 

  Customization    
- [ Introduction ](https://spatie.be/docs/laravel-feed-rust/v1/introduction)
- [ Support us ](https://spatie.be/docs/laravel-feed-rust/v1/support-us)
- [ Installation ](https://spatie.be/docs/laravel-feed-rust/v1/installation-and-setup)
- [ Usage ](https://spatie.be/docs/laravel-feed-rust/v1/usage)
- [ HTTP integration ](https://spatie.be/docs/laravel-feed-rust/v1/http)
- [ Customization ](https://spatie.be/docs/laravel-feed-rust/v1/customization)
- [ Changelog ](https://spatie.be/docs/laravel-feed-rust/v1/changelog)
- [ Testing ](https://spatie.be/docs/laravel-feed-rust/v1/testing)

 Customization
=============

###  On this page 

1. [ Convert application models ](#content-convert-application-models)
2. [ Use typed configuration ](#content-use-typed-configuration)
3. [ Render a custom native view ](#content-render-a-custom-native-view)
4. [ Include discovery links in views ](#content-include-discovery-links-in-views)

Convert application models
--------------------------------------------------------------------------------------------------------------------------------------

- Implement `Feedable` on a model to move its fields into an entry, or on a reference to keep the model available.
- Models keep their own database connection and query options. There is no package model or job to subclass.

```
use laravel::time::ZonedDateTime;
use spatie_feed::{Feed, FeedItem, Feedable};

struct Article {
    slug: String,
    title: String,
    html: String,
    published_at: ZonedDateTime,
}

impl Feedable for Article {
    fn into_feed_item(self) -> FeedItem {
        FeedItem::new(
            &self.slug, self.title, self.published_at,
            format!("/articles/{}", self.slug), self.html, "Editorial team",
        )
    }
}

let feed = Feed::new("Articles", "https://example.com/feed")?.items(articles);
```

- `items` and `register_feed` both accept models implementing `Feedable`.
- Conversion is synchronous. Keep fallible or asynchronous work in the loader, where errors retain their framework status.
- Capture query arguments in the loader closure. Use an ordinary native handler when it needs request extractors.

Use typed configuration
-----------------------------------------------------------------------------------------------------------------------------

- Store `Feed` directly as a native config section. For several feeds, define an application-owned section containing them.
- Group defaults and routes with `spatie_package_tools::Package`. Application configuration overrides package defaults before routes register.
- Add `spatie-laravel-package-tools = { git = "https://github.com/spatie/laravel-package-tools-rust" }` to your dependencies for this example:

```
use laravel::{Application, config::Env};
use spatie_feed::{Feed, FeedItem, Format, register_feed};
use spatie_package_tools::Package;

let defaults = Feed::new("News", "https://example.com/syndication/news")?
    .format(Format::Rss)
    .language("en-US");
let app = Application::configure()
    .with_config(|config, env| {
        if let Some(title) = env.get::<String>("FEED_TITLE") {
            let mut feed = config.section::<Feed>().clone();
            feed.title = title;
            config.set(feed);
        }
    })
    .with_providers(move |providers| {
        providers.add(Package::new("news-feed")
            .config_defaults(defaults)
            .routes(|routing, config| {
                let feed = config.section::<Feed>().clone();
                routing.web(move |router| {
                    router.prefix("/syndication").group(|router| {
                        register_feed(router, "/news", feed, || async {
                            Ok(Vec::<FeedItem>::new())
                        });
                    });
                });
            }));
    })
    .create();
```

Render a custom native view
-----------------------------------------------------------------------------------------------------------------------------------------

- Set `feed.view = Some("feeds.news".into())` to render an application template instead of the built-in serializer.
- Use native view directories, namespaces, embedded templates and engines. With `minijinja` enabled, `feeds.news` resolves to `resources/views/feeds/news.html`.

The view receives:

- `meta` with `id`, `link`, `home_page_url`, `title`, `description`, `language`, `image` and `updated`.
- `items` in their original order, with `id`, `link`, `title`, `summary`, `updated`, `author_name`, `author_email`, `categories`, `image` and `enclosure`.
- `enclosure` contains `url`, `mime_type` and `length`, or is null when absent.
- `stylesheet`, the configured optional stylesheet string.
- Entry and image URLs resolve before rendering.
- Dates use RFC 2822 for RSS, RFC 3339 for Atom and JSON. Empty Atom feeds still need `feed.updated`.
- Other absent values are null. HTML summaries remain unescaped data; the template controls their encoding.

For example, an application-specific XML template can use normal template escaping:

```
<news title="{{ meta.title }}">
{% for item in items %}
  <article id="{{ item.id }}">{{ item.summary }}</article>
{% endfor %}
</news>
```

- The response keeps the format's Content-Type unless `feed.content_type` overrides it.
- Native view errors become `Error::View` and produce HTTP 500 responses.
- Custom views require an active application scope, such as a route handler or `#[laravel::test(app = create)]`.

Include discovery links in views
--------------------------------------------------------------------------------------------------------------------------------------------------------

- Call `discovery_links([&atom, &rss, &json])?` and pass the result to a native view with `.with_html("feed_links", laravel::view::Html::raw(links))`.
- Insert `{{ feed_links }}` inside a MiniJinja layout's head. `with_html` marks the escaped markup as trusted; ordinary string data would escape the tags again.
- See [usage](usage) for media type overrides and XML stylesheets.

 A good
match?
-------------

### What we do best

- All things Laravel
- Custom frontend components
- Building APIs
- AI-powered features
- Simplifying things
- Clean solutions
- Integrating services

### Not our cup of tea

- WordPress themes
- Cutting corners
- Free mockups to win a job
- "Just execute the briefing"

 In short: we'd like to be a **substantial part** of your project.

 [ Get in touch via email ](mailto:info@spatie.be?subject=A%20good%20match%21&body=Tell%20us%20as%20much%20as%20you%20can%20about%0A-%20your%20online%20project%0A-%20your%20planning%0A-%20your%20budget%0A-%20%E2%80%A6%0A%0AAnything%20that%20helps%20us%20to%20start%20straightforward%21)
