Grab a well deserved cup of coffee, you're two steps away from creating a page with dynamic content.
+Grab a well deserved cup of coffee, you're = $isConfigured ? 'two' : 'three'; ?> steps away from creating a page with dynamic content.
If you haven't yet, create a prismic.io content repository. A repository is where your website’s content will live. Simply create one by choosing a repository name and a plan. We’ve got a variety of plans including our favorite, Free!
Replace the repository url in your config file with your-repo-name.prismic.io
+Replace the repository url in your prismic config file with your-repo-name
.prismic.io.
-// In config/prismic.php
-"url" => "https://your-repo-name.prismic.io/api",
-
+ // In config/prismic.php
+"url" => "https://your-repo-name.prismic.io/api",
To add a page to your project, you need to first specify a route. The route contains the URL and performs queries for the needed content.
In the following example we set a "/page/{uid}" URL to fetch content of custom type "page" by its UID. The route then calls the "page" template and passes it the retrieved content.
+ To add a page to your project, you need to first specify a route. The route contains the URL and performs queries for the needed content.
+
+ Note that you will need to include a UID field in your Custom Type in order for this to work.
+ In the following example we set a /page/:uid
URL to fetch content of your custom type by its UID. The route then calls the page
template and passes it the retrieved content. Make sure to replace <your-custom-type-id>
below with the API ID of your Custom Type.
+
+
-// Add a new route in routes/web.php
+ // Add a new route in routes/web.php
Route::get('/page/{uid}', function ($uid, Request $request) {
- // Query the page by the uid by using the getByUID function
- $pageContent = $request['api']->getByUID('page', $uid);
- // $pageContent is a document, or null if there is no match
+ // Query the page by the uid by using the getByUID function
+ $document = $request['api']->getByUID('<your-custom-type-id>', $uid);
+ // $document is the returned document object, or null if there is no match
- // Render the page. 'page' is the name of your blade template view (resources/views/page.blade.php)
- return view('page', [ 'pageContent' => $pageContent ]);
-});
-
+ // Render the page. 'page' is the name of your blade template view (resources/views/page.blade.php)
+ return view('page', [ 'document' => $document ]);
+});
To discover all the functions you can use to query your documents go to the prismic documentation.
+Now all that's left to be done is insert your content into the template.
You can get the content using the pageContent object we defined above. Each content field is accessed using the custom type API-ID and the field key defined in the custom type (for example 'page.image').
+ Now all that's left to be done is to insert your content into the template.
+
+ You can get the content using the document
variable we defined above. Each content field is accessed using the custom type API-ID and the content field API ID defined in the custom type. For example if you had a custom type with the API ID of page
which has an image field with the API ID main_image
, then you would use page.main_image
.
+
-{{-- Create template resources/views/page.blade.php --}}
+ {{-- Create template resources/views/page.blade.php --}}
@extends('layout')
{{-- Create a content section --}}
@section('content')
- <div>
- {{-- This is how to get an image into your template --}}
- <img src="{{ $pageContent->getImage('{{'page.image'}}')->getUrl() }}" />
-
- {{-- This is how to get structured text fields into your template --}}
- {!! $pageContent->getStructuredText('{{'page.title'}}')->asHtml($linkResolver) !!}
- {!! $pageContent->getStructuredText('{{'page.description'}}')->asHtml($linkResolver) !!}
- </div>
-@stop
-
+ <div>
+ {{-- This is how to get an image into your template --}}
+ <img src="{{ $document->getImage('custom_type_id.image_field_id')->getUrl() }}" />
+
+ {{-- This is how to get structured text fields into your template --}}
+ {!! $document->getStructuredText('custom_type_id.title_field_id')->asHtml($linkResolver) !!}
+ {!! $document->getStructuredText('custom_type_id.description_field_id')->asHtml($linkResolver) !!}
+ </div>
+@stop
To discover how to get all the different content fields, go to the prismic documentation.