glhd/laravel-prismoquent

Prismoquent lets you access Prismic.io using the same methods you're used to in Eloquent.

0.5.1 2019-05-09 19:42 UTC

This package is auto-updated.

Last update: 2021-11-10 01:06:35 UTC


README

CircleCI Build Status Code Coverage Status Stable version on Packagist Dev version on Packagist Code Style Status License

This package provides a mostly Eloquent-compatible Model that you can use to access content from Prismic.io as though it were a standard Eloquent model.

App/Page.php

class Page extends \Galahad\Prismoquent\Model
{
	// Automatically inferred from class name ("page") if left unset
	protected $type;
	
	// Cast RichText to text or HTML (all other cast types also supported)
	protected $casts = [
		'title' => 'text',
		'body' => 'html',
	];
	
	// Resolve links as though they were relationships
	public function authorLinkResolver() : ?Person
	{
		return $this->hasOne('author', Person::class);
	}
	
	// Also supports repeating groups of links
	public function similarPagesLinkResolver()
	{
		return $this->hasMany('similar_pages.page', static::class);
	}
}

App/Http/Controllers/PageController.php

class PageController extend Controller
{
	public function show(Page $page)
	{
		return view('page.show', compact('page'));
	}
}

resources/views/page/show.blade.php

<h1>{{ $page->title }}</h1>

<div class="page-body">
	{{ $page->body }}
</div>
// Familiar API
$page = Page::where('document.id', 'W2N5Dx8AAD1TPaYt')->first();
$page = Page::find('W2N5Dx8AAD1TPaYt');
$page = Page::findOrFail('W2N5Dx8AAD1TPaYt');

echo "<h1>{$page->title}</h1>";
echo $page->body;

echo "<p>Written by {$page->author->name}</p>"
echo "<h2>Similar Pages</h2>";

foreach ($page->similar_pages as $similar_page) {
	echo "<h3>{$similar_page->title}</h3>";
	echo "<p>{$similar_page->meta_description}</p>";
}

// With full support for all Prismic predicates
Page::where('my.page.body', 'fulltext', 'laravel')->get();

Warning: Active Development

This project is still in active development and may have many bugs. Use at your own risk!

Installation

You can install the package via composer:

composer require glhd/laravel-prismoquent

Usage

See above for a basic example. More details coming soon.

Configuration

Looks for config in your services.php file:

return [
	'prismic' => [
		'endpoint' => env('PRISMIC_ENDPOINT'), // Required
		'api_token' => env('PRISMIC_API_TOKEN'), // Optional, depending on your Prismic permissions
		'webhook_secret' => env('PRISMIC_WEBHOOK_SECRET'), // Optional, if you're using build-in controller
		'register_controller' => false, // Set to false to disable Webhook controller
    ]
];

Link Resolution

You can register link resolvers as either a callable or a route name:

// In your AppServiceProvider
Prismic::registerResolver('page', 'pages.show');
Prismic::registerResolver('person', function(DocumentLink $link) {
	return url('/people/'.$link->getUid());
});

// In your web.php route file
Route::get('/pages/{page}', 'PageController@show')->name('pages.show');

If you do not set up a resolver, Prismoquent will try a resource route for your document. So Page will try route('pages.show', $uid) or NewsItem will try route('news_items.show', $uid).

Once your resolvers are defined, you can resolve links in any Prismic Fragment using:

$html = Prismic::asHtml($fragment);

Blade Directives

{{-- Will render slice object using views/slices/slice-type.blade.php --}}
@slice($object_implementing_slice_tnterface)

{{-- Will render all slices in slice zone using @slice directive --}}
@slice($slice_zone_object)

{{-- Converts frament to HTML using link resolver --}}
@asHtml($fragment)

{{-- Converts frament to plain text --}}
@asText($fragment)

{{-- Converts a DocumentLink fragment to the resolved URL --}}
@resolveLink($documentLink)

License

The MIT License (MIT). Please see License File for more information.