Getting started

Installation

1
composer require astrotomic/stancy
2
php artisan vendor:publish --provider="Spatie\Sheets\SheetsServiceProvider" --tag=config
3
mkdir -p ./resources/content/static
4
mkdir -p ./views/content
5
mkdir -p ./app/Pages
Copied!
config/filesystems.php
config/sheets.php
1
<?php
2
​
3
return [
4
// ...
5
'disks' => [
6
// ...
7
'static' => [
8
'driver' => 'local',
9
'root' => resource_path('content/static'),
10
],
11
],
12
];
Copied!
1
<?php
2
​
3
return [
4
'default_collection' => 'static',
5
​
6
'collections' => [
7
'static',
8
],
9
];
Copied!
This is everything you have to do to prepare your first static sheet/page collection.

Home

This will be an example for a simple static page - like your home/front page.
1
touch ./resources/content/static/home.md
2
touch ./views/content/home.blade.php
3
touch ./app/Pages/Home.php
Copied!
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.
home.md
Home.php
home.blade.php
1
---
2
_view: content.static.home
3
_pageData: \App\Pages\Home
4
---
5
# Home
6
​
7
This is my first awesome Stancy page.
Copied!
1
<?php
2
​
3
namespace App\Pages;
4
​
5
use Astrotomic\Stancy\Contracts\Routable;
6
use Astrotomic\Stancy\Models\PageData;
7
use Astrotomic\Stancy\Traits\PageHasContent;
8
use Astrotomic\Stancy\Traits\PageHasSlug;
9
use Astrotomic\Stancy\Traits\PageHasUrl;
10
​
11
class Home extends PageData implements Routable
12
{
13
use PageHasSlug, PageHasContent, PageHasUrl;
14
​
15
public function getUrl(): string
16
{
17
return route('static.home');
18
}
19
}
Copied!
1
@extends('master')
2
​
3
@section('content')
4
{!! $contents !!}
5
@endsection
Copied!
Now you have to add the route to your routes/web.php
routes/web.php
1
<?php
2
​
3
use Astrotomic\Stancy\Facades\PageFactory;
4
use Illuminate\Support\Facades\Route;
5
​
6
Route::get('/', function () {
7
return PageFactory::makeFromSheetName('static', 'home');
8
})->name('static.home');
Copied!

Sitemap

To add your page to the sitemap and return it if someone access /sitemap.xml you have to do the following.
routes/web.php
1
<?php
2
​
3
use Astrotomic\Stancy\Facades\SitemapFactory;
4
use Illuminate\Support\Facades\Route;
5
​
6
Route::get('/sitemap.xml', function () {
7
return SitemapFactory::makeFromSheetList(['static']);
8
})->name('sitemap');
Copied!
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.
1
touch ./resources/content/static/blog.md
2
touch ./views/content/blog.blade.php
3
touch ./app/Pages/Blog.php
Copied!
blog.md
Blog.php
blog.blade.php
1
---
2
_view: static.blog
3
_pageData: \App\Pages\Blog
4
_sheets:
5
posts: blog:*
6
---
7
# Blog
8
​
9
This is my fantastic blog index.
Copied!
1
<?php
2
​
3
namespace App\Pages;
4
​
5
use Astrotomic\Stancy\Contracts\Routable;
6
use Astrotomic\Stancy\Models\PageData;
7
use Astrotomic\Stancy\Traits\PageHasContent;
8
use Astrotomic\Stancy\Traits\PageHasSlug;
9
use Astrotomic\Stancy\Traits\PageHasUrl;
10
​
11
class Blog extends PageData implements Routable
12
{
13
use PageHasSlug, PageHasContent, PageHasUrl;
14
​
15
/** @var \App\Pages\Post[] */
16
public $posts;
17
​
18
public function getUrl(): string
19
{
20
return route('static.blog');
21
}
22
}
Copied!
1
@extends('master')
2
​
3
@section('content')
4
<div class="row">
5
@foreach(collect($posts)->sortByDesc('date') as $post)
6
<div class="col-12 col-md-4 col-xl-3">
7
<div class="card">
8
<img src="{{ url($post->image) }}" class="card-img-top">
9
<div class="card-body">
10
<h3 class="card-title">{{ $post->title }}</h3>
11
<div><small>{{ $post->date->format('Y-m-d H:i') }}</small></div>
12
<div class="card-text mb-3">{!! Illuminate\Support\Str::words(strip_tags($post->contents), 20) !!}</div>
13
<a href="{{ $post->getUrl() }}" class="btn btn-primary">open post</a>
14
</div>
15
</div>
16
</div>
17
@endforeach
18
</div>
19
@endsection
Copied!
Before you can test this we will have to add our first post to the blog collection.
1
mkdir -p ./resources/content/blog
Copied!
config/filesystems.php
config/sheets.php
1
<?php
2
​
3
return [
4
// ...
5
'disks' => [
6
// ...
7
'blog' => [
8
'driver' => 'local',
9
'root' => resource_path('content/blog'),
10
],
11
],
12
];
Copied!
1
<?php
2
​
3
return [
4
'default_collection' => 'static',
5
​
6
'collections' => [
7
'static',
8
'blog',
9
],
10
];
Copied!
1
touch ./resources/content/blog/first-post.md
2
touch ./views/content/post.blade.php
3
touch ./app/Pages/Post.php
Copied!
first-post.md
Post.php
post.blade.php
1
---
2
_view: blog.post
3
_pageData: \App\Pages\Post
4
title: my first post
5
image: https://via.placeholder.com/1920x1080/E91E63/FFFFFF?text=Stancy
6
date: 2019-09-04 17:31
7
---
8
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.
9
​
10
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.
Copied!
1
<?php
2
​
3
namespace App\Pages;
4
​
5
use Astrotomic\Stancy\Contracts\Routable;
6
use Astrotomic\Stancy\Models\PageData;
7
use Astrotomic\Stancy\Traits\PageHasContent;
8
use Astrotomic\Stancy\Traits\PageHasDate;
9
use Astrotomic\Stancy\Traits\PageHasSlug;
10
use Astrotomic\Stancy\Traits\PageHasUrl;
11
use Carbon\Carbon;
12
​
13
class Post extends PageData implements Routable
14
{
15
use PageHasSlug, PageHasContent, PageHasDate, PageHasUrl;
16
​
17
/** @var string */
18
public $title;
19
​
20
/** @var string */
21
public $image;
22
​
23
public function __construct(array $parameters)
24
{
25
if (isset($parameters['date']) && is_string($parameters['date'])) {
26
$parameters['date'] = Carbon::make($parameters['date']);
27
}
28
​
29
parent::__construct($parameters);
30
}
31
​
32
public function getUrl(): string
33
{
34
return route('blog.post', ['slug' => $this->slug]);
35
}
36
}
Copied!
1
@extends('master')
2
​
3
@section('content')
4
<img src="{{ url($image) }}" class="img-fluid">
5
<h1 class="">{{ $title }}</h1>
6
{!! $contents !!}
7
@endsection
Copied!
routes/web.php
1
<?php
2
​
3
use Astrotomic\Stancy\Facades\PageFactory;
4
use Illuminate\Support\Facades\Route;
5
​
6
Route::get('/blog/{slug}', function (string $slug) {
7
return PageFactory::makeFromSheetName('blog', $slug);
8
})->name('blog.post');
9
​
10
Route::get('/sitemap.xml', function () {
11
return SitemapFactory::makeFromSheetList([
12
'static',
13
'blog', // added
14
]);
15
});
Copied!
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.
1
php artisan vendor:publish --provider="Spatie\Feed\FeedServiceProvider" --tag=config
Copied!
config/feed.php
routes/web.php
master.blade.php
1
<?php
2
​
3
use Astrotomic\Stancy\Contracts\FeedFactory;
4
​
5
return [
6
'feeds' => [
7
'blog.atom' => [
8
'items' => [FeedFactory::class.'@makeFromSheetCollectionName', 'blog'],
9
'url' => 'feed/blog.atom',
10
'title' => 'Stancy Blog Feed',
11
'description' => 'This is the Stancy Laravel demo blog feed.',
12
'language' => 'en-US',
13
'view' => 'feed::atom',
14
'type' => 'application/atom+xml',
15
],
16
'blog.rss' => [
17
'items' => [FeedFactory::class.'@makeFromSheetCollectionName', 'blog'],
18
'url' => 'feed/blog.rss',
19
'title' => 'Blog Feed',
20
'description' => 'This is the Stancy Laravel demo blog feed.',
21
'language' => 'en-US',
22
'view' => 'feed::rss',
23
'type' => 'application/rss+xml',
24
],
25
],
26
];
Copied!
1
<?php
2
​
3
use Illuminate\Support\Facades\Route;
4
​
5
Route::feeds();
Copied!
1
<!DOCTYPE html>
2
<html>
3
<head>
4
<!-- head -->
5
@include('feed::links')
6
</head>
7
<body>
8
<!-- body -->
9
</body>
10
</html>
Copied!
Now you have to prepare the \App\Pages\Post page data class.
Post.php
1
<?php
2
​
3
namespace App\Pages;
4
​
5
use Astrotomic\Stancy\Contracts\Routable;
6
use Astrotomic\Stancy\Models\PageData;
7
use Illuminate\Support\Str;
8
use Spatie\Feed\FeedItem;
9
​
10
class Post extends PageData implements Routable
11
{
12
// ...
13
​
14
public function toFeedItem(): FeedItem
15
{
16
return FeedItem::create()
17
->id($this->slug)
18
->link($this->getUrl())
19
->title($this->title)
20
->author('Gummibeer, [email protected]')
21
->updated($this->date)
22
->summary(Str::words(strip_tags($this->contents), 50, ''));
23
}
24
​
25
// ...
26
}
Copied!
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. πŸš€
Last modified 1yr ago