Getting started

Installation

composer require astrotomic/stancy
php artisan vendor:publish --provider="Spatie\Sheets\SheetsServiceProvider" --tag=config
mkdir -p ./resources/content/static
mkdir -p ./views/content
mkdir -p ./app/Pages
<?php

return [
    // ...
    'disks' => [
        // ...
        'static' => [
            'driver' => 'local',
            'root' => resource_path('content/static'),
        ],
    ],
];

This is everything you have to do to prepare your first static sheet/page collection.

pageInstallation

Home

This will be an example for a simple static page - like your home/front page.

touch ./resources/content/static/home.md
touch ./views/content/home.blade.php
touch ./app/Pages/Home.php

These three files will represent your home page. The markdown file will hold the data/content, the view will render it and the PHP class will validate it and provide additional features.

---
_view: content.static.home
_pageData: \App\Pages\Home
---
# Home

This is my first awesome Stancy page.

Now you have to add the route to your routes/web.php

<?php

use Astrotomic\Stancy\Facades\PageFactory;
use Illuminate\Support\Facades\Route;

Route::get('/', function () {
    return PageFactory::makeFromSheetName('static', 'home');
})->name('static.home');
pageContent

Sitemap

To add your page to the sitemap and return it if someone access /sitemap.xml you have to do the following.

<?php

use Astrotomic\Stancy\Facades\SitemapFactory;
use Illuminate\Support\Facades\Route;

Route::get('/sitemap.xml', function () {
    return SitemapFactory::makeFromSheetList(['static']);
})->name('sitemap');

This will add all pages in the static collection to your sitemap.

pageSitemap

Blog

One of the most common examples for a page collection is a blog. You need an index page and a detail one for every article.

touch ./resources/content/static/blog.md
touch ./views/content/blog.blade.php
touch ./app/Pages/Blog.php
---
_view: static.blog
_pageData: \App\Pages\Blog
_sheets:
    posts: blog:*
---
# Blog

This is my fantastic blog index.

Before you can test this we will have to add our first post to the blog collection.

mkdir -p ./resources/content/blog
<?php

return [
    // ...
    'disks' => [
        // ...
        'blog' => [
            'driver' => 'local',
            'root' => resource_path('content/blog'),
        ],
    ],
];
touch ./resources/content/blog/first-post.md
touch ./views/content/post.blade.php
touch ./app/Pages/Post.php
---
_view: blog.post
_pageData: \App\Pages\Post
title: my first post
image: https://via.placeholder.com/1920x1080/E91E63/FFFFFF?text=Stancy
date: 2019-09-04 17:31
---
Add you viewing ten equally believe put. Separate families my on drawings do oh offended strictly elegance. Perceive jointure be mistress by jennings properly. An admiration at he discovered difficulty continuing. We in building removing possible suitable friendly on. Nay middleton him admitting consulted and behaviour son household. Recurred advanced he oh together entrance speedily suitable. Ready tried gay state fat could boy its among shall.

Both rest of know draw fond post as. It agreement defective to excellent. Feebly do engage of narrow. Extensive repulsive belonging depending if promotion be zealously as. Preference inquietude ask now are dispatched led appearance. Small meant in so doubt hopes. Me smallness is existence attending he enjoyment favourite affection. Delivered is to ye belonging enjoyment preferred. Astonished and acceptance men two discretion. Law education recommend did objection how old.
<?php

use Astrotomic\Stancy\Facades\PageFactory;
use Illuminate\Support\Facades\Route;

Route::get('/blog/{slug}', function (string $slug) {
    return PageFactory::makeFromSheetName('blog', $slug);
})->name('blog.post');

Route::get('/sitemap.xml', function () {
    return SitemapFactory::makeFromSheetList([
        'static',
        'blog', // added
    ]);
});

Now you are done. If you want to add a new post just add a new markdown file to the blog collection and fill all required data.

Feed

After you now have your first collection of same type pages (blog) it's useful to put them in a feed and link it in your HTML.

php artisan vendor:publish --provider="Spatie\Feed\FeedServiceProvider" --tag=config
<?php

use Astrotomic\Stancy\Contracts\FeedFactory;

return [
    'feeds' => [
        'blog.atom' => [
            'items' => [FeedFactory::class.'@makeFromSheetCollectionName', 'blog'],
            'url' => 'feed/blog.atom',
            'title' => 'Stancy Blog Feed',
            'description' => 'This is the Stancy Laravel demo blog feed.',
            'language' => 'en-US',
            'view' => 'feed::atom',
            'type' => 'application/atom+xml',
        ],
        'blog.rss' => [
            'items' => [FeedFactory::class.'@makeFromSheetCollectionName', 'blog'],
            'url' => 'feed/blog.rss',
            'title' => 'Blog Feed',
            'description' => 'This is the Stancy Laravel demo blog feed.',
            'language' => 'en-US',
            'view' => 'feed::rss',
            'type' => 'application/rss+xml',
        ],
    ],
];

Now you have to prepare the \App\Pages\Post page data class.

<?php

namespace App\Pages;

use Astrotomic\Stancy\Contracts\Routable;
use Astrotomic\Stancy\Models\PageData;
use Illuminate\Support\Str;
use Spatie\Feed\FeedItem;

class Post extends PageData implements Routable
{
    // ...

    public function toFeedItem(): FeedItem
    {
        return FeedItem::create()
            ->id($this->slug)
            ->link($this->getUrl())
            ->title($this->title)
            ->author('Gummibeer, dev.gummibeer@gmail.com')
            ->updated($this->date)
            ->summary(Str::words(strip_tags($this->contents), 50, ''));
    }

    // ...
}

And you are done. You will have two feeds (RSS and Atom) now. If you only want one just remove the other one from the config.

pageFeed Atom/RSS

Congratulations! 🎉 You have your first Stancy setup and are ready to go. 🚀

Last updated