Usage | laravel-feed-go | Spatie       

 SPATIE  

laravel-feed-go
===============

spatie.be/open-source

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

 Version   v1      

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

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

 Usage
=====

###  On this page 

1. [ Create feed items ](#content-create-feed-items)
2. [ Choose a format ](#content-choose-a-format)
3. [ URLs, ordering and timestamps ](#content-urls-ordering-and-timestamps)
4. [ Escaping and errors ](#content-escaping-and-errors)
5. [ XML stylesheets ](#content-xml-stylesheets)

Create feed items
-----------------------------------------------------------------------------------------------------------

Supply a stable ID, title, update timestamp, canonical link, summary HTML and author name. Use a struct literal instead of PHP's arrays or fluent setters.

```
item := feed.Item{
    ID: "42",
    Title: "A conversation",
    Updated: time.Date(2026, 11, 30, 9, 0, 0, 0, time.FixedZone("Brussels", 3600)),
    Link: "/42-a-conversation",
    Summary: "<p>Listen & enjoy.</p>",
    AuthorName: "Freek",
    AuthorEmail: "freek@spatie.be",
    Categories: []string{"Go", "Podcasts"},
    Enclosure: &feed.Enclosure{
        URL: "/audio/42.mp3",
        MIMEType: "audio/mpeg",
        Length: 123456,
    },
}
f := feed.Feed{
    Title: "Conversations",
    URL: "https://example.com/feed",
    Format: feed.RSS,
    Items: []feed.Item{item},
}
body, err := f.Render(ctx)
if err != nil {
    return err
}
// Write body to storage, or return f.Response(r) from a Laravel handler.
_ = body
```

Import `github.com/spatie/laravel-feed-go` and `time`. `ctx` is a context, normally a Laravel request, job or command context. Built-in rendering also works with `context.Background()`.

`ID`, `Title`, `Link` and `AuthorName` must be nonempty. `Updated` must be nonzero and have a year from 1 through 9999. `Summary` may be empty. Models can implement `Feedable`, or loaders can return items directly. See [customization](customization).

Choose a format
-----------------------------------------------------------------------------------------------------

An unset `Format` means Atom. Set `feed.RSS` or `feed.JSON` to select another format. Invalid formats return an error, and configured routes reject them at startup.

Atom and RSS responses use `application/xml;charset=UTF-8`. JSON uses `application/json;charset=UTF-8`. Discovery links use `application/atom+xml`, `application/rss+xml` and `application/feed+json` respectively. `ContentType` and `DiscoveryType` override those defaults independently.

`Image` on a feed renders as an Atom logo, RSS image or JSON icon. An item's `Image` is used by JSON Feed. An `Enclosure` requires its URL and MIME type, and contains its byte length. All three formats include enclosures. JSON Feed lists distinct authors by name and keeps item categories as tags.

URLs, ordering and timestamps
---------------------------------------------------------------------------------------------------------------------------------------------

`URL` is the absolute HTTP(S) self URL and Atom ID. Package routes fill it from the request path when unset, without its query string. Direct rendering requires it explicitly.

Relative IDs, links, images and enclosures resolve against `HomePageURL`. It defaults to the self URL's origin. ID `42` under `https://example.com/feed/news` becomes `https://example.com/42`. Absolute IDs such as `urn:post:42` remain unchanged. Set `HomePageURL: "https://example.com/blog/"` to keep relative paths under `/blog/`. Paths starting with `/` still resolve at the host root.

Entries keep their supplied order and duplicates. The feed update timestamp is the latest entry by instant, not by local clock value. Atom and JSON timestamps use whole-second RFC 3339 and retain their UTC offset. RSS uses RFC 1123 with numeric offsets. UTC offsets must be whole minutes and less than 24 hours. Convert historical second-resolution offsets to UTC before rendering. Rendering does not mutate the supplied items.

Empty Atom feeds require an explicit `Updated`, for example your collection's last publication or removal timestamp. Without it, rendering returns `feed.ErrMissingUpdated`. Nonempty feeds always use the latest entry. Empty RSS and JSON feeds need no timestamp.

Escaping and errors
-----------------------------------------------------------------------------------------------------------------

Pass raw rendered HTML, not XML-escaped strings, as `Summary`. Atom stores it in `summary type="html"`, RSS in `description` and JSON in `content_html`. XML escaping preserves the original content after parsing, including CDATA terminators, line breaks and query strings. JSON uses JSON escaping rather than HTML entities.

XML 1.0 forbidden characters and invalid UTF-8 return errors instead of silently changing content. HTML is not sanitized. Supply content safe for your audience.

`Render` returns URL, validation, cancellation and custom renderer errors. `errors.Is(err, feed.ErrMissingUpdated)` identifies an empty Atom feed without a timestamp. A failed render becomes an HTTP error through `Response`, never a successful empty feed.

XML stylesheets
-----------------------------------------------------------------------------------------------------

Set `Stylesheet: "/feed.xsl"` to include a stylesheet processing instruction in Atom or RSS. The URL resolves against `HomePageURL`. JSON ignores the stylesheet. Unlike PHP's published Atom presentation assets, stylesheets and their CSS remain application-owned. No stylesheet is injected by default.

 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)
