If you already have an existing Laravel project that you want to integrate with Prismic, then you simply need to add the PHP Prismic development kit library as a dependency. Here we will show you all the steps needed to get Prismic integrated into your site.
A content repository is where you can define, edit, and publish your website content.
Next you will need to model your content, create your custom types, and publish some documents to your content repository.
Now, let’s take a look at how to retrieve and integrate this new content with your project.
Now let’s add the Prismic PHP kit as a dependency to your project. Launch the terminal (command prompt or similar on Windows), and point it to your project location.
Note that you will need to make sure to first have Composer installed before running this command. Check out the Composer Getting Started page for installation instructions.
Run the following command:
composer require prismic/php-sdk
To use the Prismic PHP library, you will need to include an instance of it. Simply add the following code:
<?php
include_once __DIR__.'/vendor/autoload.php';
use Prismic\Api;
use Prismic\LinkResolver;
use Prismic\Predicates;
Now we can query your Prismic repository and retrieve the content as shown below:
<?php
$api = Api::get("https://your-repo-name.cdn.prismic.io/api/v2");
$response = $api->query(Predicates::at('document.type', 'page'));
If you are using a private repository, then you’ll need to generate an access token and then include it like this:
<?php
$url = "https://your-repo-name.cdn.prismic.io/api/v2";
$token = "MC5XUkgtxelvOGEBdVViSEla.E--_xe-_qe-vUXvv7...1r77-9FgXv";
$api = Api::get($url, $token);
$response = $api->query(Predicates::at('document.type', 'page'));
To learn more about querying the API, check out the How to Query the API page.
When querying a Prismic repository, your results will be paginated. By default, there are 20 documents per page in the results. You can read more about how to manipulate the pagination in the Pagination for Results page.
Once you have retrieved your content, you can include the content in your template using the helper functions in the PHP development kit. Here is a simple example:
<?php
use Prismic\Dom\RichText;
$document = $response->results[0];
?>
<section>
<h1>{{ RichText::asText($document->data->title) }}</h1>
<img src="{{ $document->data->image->url }}" alt="{{ $document->data->image->alt }}" />
<div>
{!! RichText::asHtml($document->data->description) !!}
</div>
</section>
You can read more about templating your content in the Templating section of the documentation.
In order to take advantage of all that Prismic has to offer, check out the Previews and the Prismic Toolbar page to learn how to add these great features to your project!