PHP code example of spatie / sheets
1. Go to this page and download the library: Download spatie/sheets library . Choose the download type require .
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
spatie / sheets example snippets
class SheetController
{
public function index(Sheets $sheets)
{
return view('sheet', [
'sheet' => $sheets->get('home'),
]);
}
public function show(string $id, Sheets $sheets)
{
return view('sheet', [
'sheet' => $sheets->get($id),
]);
}
}
use Sheets;
Sheets::all();
sheets()->all();
use Spatie\Sheets\Sheets;
class SheetsController
{
public function index(Sheets $sheets)
{
return view('sheets', [
'sheets' => $sheets->all(),
]);
}
}
// config/filesystems.php
return [
'disks' => [
// ...
'posts' => [
'driver' => 'local',
'root' => base_path('posts'),
],
],
];
// config/sheets.php
return [
'collections' => ['posts'],
];
$repository = Sheets::collection('posts');
$repository->all();
Sheets::collection('posts')->get('hello-world');
$sheet = Sheets::collection('posts')->get('hello-world');
echo $sheet->slug;
// 'hello-world'
echo $sheet->title;
// 'Hello, world!'
echo $sheet->contents;
// '<h1>Hello, world!</h1><p>Welcome to Sheets!</p>'
// config/sheets.php
return [
'collections' => [
'pages' => [
'disk' => null, // Defaults to collection name
'sheet_class' => Spatie\Sheets\Sheet::class,
'path_parser' => Spatie\Sheets\PathParsers\SlugParser::class,
'content_parser' => Spatie\Sheets\ContentParsers\MarkdownWithFrontMatterParser::class,
'extension' => 'md',
],
],
];
// config/sheets.php
return [
'collections' => [
'pages' => [
'disk' => null, // Uses the 'pages' disk
],
],
];
// config/sheets.php
return [
'collections' => [
'pages' => [
'disk' => 'sheets', // Uses the 'sheets' disk
],
],
];
$sheet = Sheets::collection('page')->get('hello-world');
echo $sheet->slug;
// 'hello-world'
namespace App;
use Spatie\Sheets\Sheet;
class Page extends Sheet
{
public function getUrlAttribute(): string
{
return url($this->slug);
}
}
// config/sheets.php
return [
'collections' => [
'pages' => [
'sheet_class' => App\Page::class,
],
],
];
$sheet = Sheets::collection('pages')->get('hello-world');
echo $sheet->url;
// 'https://example.app/hello-world'
namespace Spatie\Sheets\PathParsers;
use Spatie\Sheets\PathParser;
class SlugParser implements PathParser
{
public function parse(string $path): array
{
return ['slug' => explode('.', $path)[0]];
}
}
// config/sheets.php
return [
'collections' => [
'posts' => [
'path_parser' => Spatie\Sheets\PathParsers\SlugWithDateParser::class,
],
],
];
class MarkdownWithFrontMatterParser implements ContentParser
{
/** @var \League\CommonMark\CommonMarkConverter */
protected $commonMarkConverter;
public function __construct(CommonMarkConverter $commonMarkConverter)
{
$this->commonMarkConverter = $commonMarkConverter;
}
public function parse(string $contents): array
{
$document = YamlFrontMatter::parse($contents);
return array_merge(
$document->matter(),
['contents' => $this->commonMarkConverter->convert($document->body())]
);
}
}
// config/sheets.php
return [
'collections' => [
'pages' => [
'content_parser' => Spatie\Sheets\ContentParsers\MarkdownParser::class,
],
],
];
// Return all sheets in the default collection
Sheets::all();
return [
'default_collection' => null,
'collections' => [
'pages',
],
];
return [
'default_collection' => 'pages',
'collections' => [
'posts',
'pages',
],
];
// app/Providers/RouteServiceProvider.php
public function boot()
{
parent::boot();
Route::bind('sheet', function ($path) {
return $this->app->make(Spatie\Sheets\Sheets::class)
->get($path) ?? abort(404);
});
}
Route::get('/{sheet}', 'SheetsController@show');
class SheetsController
{
public function show(Sheet $sheet)
{
return view('sheet', ['sheet' => $sheet]);
}
}
use Spatie\Sheets\Sheets;
Route::bind('post', function ($path) {
return $this->app->make(Sheets::class)
->collection('posts')
->get($path) ?? abort(404);
});
Route::get('/', 'SheetsController@index')->where('sheet', '(.*)');
Route::get('{sheet}', 'SheetsController@show')->where('sheet', '(.*)');
php artisan vendor:publish --provider="Spatie\Sheets\SheetsServiceProvider" --tag="config"