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.
<?php
namespace App\Pages;
use Astrotomic\Stancy\Contracts\Routable;
use Astrotomic\Stancy\Models\PageData;
use Astrotomic\Stancy\Traits\PageHasContent;
use Astrotomic\Stancy\Traits\PageHasSlug;
use Astrotomic\Stancy\Traits\PageHasUrl;
class Home extends PageData implements Routable
{
use PageHasSlug, PageHasContent, PageHasUrl;
public function getUrl(): string
{
return route('static.home');
}
}
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');
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.
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.
---
_view: static.blog
_pageData: \App\Pages\Blog
_sheets:
posts: blog:*
---
# Blog
This is my fantastic blog index.
<?php
namespace App\Pages;
use Astrotomic\Stancy\Contracts\Routable;
use Astrotomic\Stancy\Models\PageData;
use Astrotomic\Stancy\Traits\PageHasContent;
use Astrotomic\Stancy\Traits\PageHasSlug;
use Astrotomic\Stancy\Traits\PageHasUrl;
class Blog extends PageData implements Routable
{
use PageHasSlug, PageHasContent, PageHasUrl;
/** @var \App\Pages\Post[] */
public $posts;
public function getUrl(): string
{
return route('static.blog');
}
}
---
_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
namespace App\Pages;
use Astrotomic\Stancy\Contracts\Routable;
use Astrotomic\Stancy\Models\PageData;
use Astrotomic\Stancy\Traits\PageHasContent;
use Astrotomic\Stancy\Traits\PageHasDate;
use Astrotomic\Stancy\Traits\PageHasSlug;
use Astrotomic\Stancy\Traits\PageHasUrl;
use Carbon\Carbon;
class Post extends PageData implements Routable
{
use PageHasSlug, PageHasContent, PageHasDate, PageHasUrl;
/** @var string */
public $title;
/** @var string */
public $image;
public function __construct(array $parameters)
{
if (isset($parameters['date']) && is_string($parameters['date'])) {
$parameters['date'] = Carbon::make($parameters['date']);
}
parent::__construct($parameters);
}
public function getUrl(): string
{
return route('blog.post', ['slug' => $this->slug]);
}
}
<?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',
],
],
];
<?php
use Illuminate\Support\Facades\Route;
Route::feeds();
<!DOCTYPE html>
<html>
<head>
<!-- head -->
@include('feed::links')
</head>
<body>
<!-- body -->
</body>
</html>
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.
Congratulations! 🎉 You have your first Stancy setup and are ready to go. 🚀