Integrating with an existing Laravel project

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.

1. Create a content repository

A content repository is where you can define, edit, and publish your website content.

Create a new repository

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.

2. Add the PHP kit as a dependency

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:

Copy
composer require prismic/php-sdk

3. Include the dependency

To use the Prismic PHP library, you will need to include an instance of it. Simply add the following code:

Copy
<?php
include_once __DIR__.'/vendor/autoload.php';

use Prismic\Api;
use Prismic\LinkResolver;
use Prismic\Predicates;

4. Get the API and query your content

Now we can query your Prismic repository and retrieve the content as shown below:

Copy
<?php
$api = Api::get("https://your-repo-name.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:

Copy
<?php
$url = "https://your-repo-name.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.

Pagination of API Results

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.

5. Add the content to your templates

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:

Copy
<?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.

6. Take advantage of Prismic features

In order to take advantage of all that Prismic has to offer, check out the in-website preview and edit-button pages to learn how to add these great features to your project!