PageData
Stancy uses spatie/data-transfer-object to validate the passed in data and allow static analysis.

Properties

Your page data class should extend \Astrotomic\Stancy\Models\PageData and define all possible attributes via public properties including a doc-tag with all possible types (FQCN for objects).
app/Pages/Blog.php
1
<?php
2
​
3
namespace App\Pages;
4
​
5
class Blog extends PageData
6
{
7
/** @var string */
8
public $title;
9
10
/** @var \Carbon\Carbon */
11
public $date;
12
​
13
/** @var \App\Pages\Post[] */
14
public $posts;
15
}
Copied!

Traits

The package comes with some ready to use traits with the most common attributes.
  • \Astrotomic\Stancy\Traits\PageHasContent adds the contents attribute (used by markdown content parsers)
  • \Astrotomic\Stancy\Traits\PageHasDate adds the date attribute (used by \Spatie\Sheets\PathParsers\SlugWithDateParser)
  • \Astrotomic\Stancy\Traits\PageHasOrder adds the order attribute (used by \Spatie\Sheets\PathParsers\SlugWithOrderParser)
  • \Astrotomic\Stancy\Traits\PageHasSlug adds the slug attribute (used by path parsers)
app/Pages/Blog.php
1
<?php
2
​
3
namespace App\Pages;
4
​
5
use Astrotomic\Stancy\Models\PageData;
6
use Astrotomic\Stancy\Traits\PageHasContent;
7
use Astrotomic\Stancy\Traits\PageHasSlug;
8
​
9
class Blog extends PageData
10
{
11
use PageHasSlug, PageHasContent;
12
}
Copied!

Routable & PageHasUrl

The package comes with a \Astrotomic\Stancy\Contracts\Routable interface which defines a getUrl() method. You can combine it with the \Astrotomic\Stancy\Traits\PageHasUrl trait which defines the toSitemapItem() method by using the getUrl() return value. It's also great to get the page url in a view or for the feed item transformation.
app/Pages/Blog.php
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\PageHasUrl;
8
​
9
class Blog extends PageData implements Routable
10
{
11
use PageHasUrl;
12
​
13
public function getUrl(): string
14
{
15
return url('/blog');
16
}
17
}
Copied!
Last modified 2yr ago