glhd / laravel-prismoquent
Prismoquent lets you access Prismic.io using the same methods you're used to in Eloquent.
Installs: 2 249
Dependents: 0
Suggesters: 0
Security: 0
Stars: 2
Watchers: 3
Forks: 0
Open Issues: 0
Requires
- php: >=7.1
- illuminate/database: 5.6.*|5.7.*|5.8.*
- illuminate/events: 5.6.*|5.7.*|5.8.*
- illuminate/pagination: 5.6.*|5.7.*|5.8.*
- illuminate/routing: 5.6.*|5.7.*|5.8.*
- illuminate/support: 5.6.*|5.7.*|5.8.*
- prismic/php-sdk: ^3.4.2
Requires (Dev)
- orchestra/testbench: ~3.6.0
- php-coveralls/php-coveralls: ^2.1
- phpunit/phpunit: ^7.0
README
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.