Laravel + Prepr CMS

This Laravel package is a provider for the Prepr API.

Basics

The SDK on GitHub
Compatible with Laravel v5x, v6x, v7x, v8x
Requires GuzzleHttp 7.0.X, Murmurhash 2.0.X

Installation

You can install the Provider as a composer package.

composer require preprio/laravel-sdk

Set-up your .env file configuration

You can set the default configuration in your .env file of you Laravel project.

PREPR_URL=https://cdn.prepr.io/
PREPR_TOKEN={{ACCESS_TOKEN}}

Laravel local caching

To make use of the caching feature of Laravel, add the following parameters to your .env file.

PREPR_CACHE=true
PREPR_CACHE_TIME=1800

Making your first request

Let's start with getting all publications from your Prepr Environment.

<?php

use Preprio\Prepr;

$apiRequest = new Prepr;

$apiRequest
    ->path('publications')
    ->query([
        'fields' => 'items'
    ])
    ->get();

if($apiRequest->getStatusCode() == 200) {

    print_r($apiRequest->getResponse());
}

To get a single publication, pass the Id to the request.

<?php

use Preprio\Prepr;

$apiRequest = new Prepr;

$apiRequest
    ->path('publications/{id}', [
        'id' => '1236f0b1-b26d-4dde-b835-9e4e441a6d09'
    ])
    ->query([
        'fields' => 'items'
    ])
    ->get();

if($apiRequest->getStatusCode() == 200) {

    print_r($apiRequest->getResponse());
}

A/B testing with Optimize

To enable A/B testing you can pass a User ID to provide a consistent result. The A/B testing feature requires the use of the cached CDN API.

To switch off A/B testing, pass NULL to the UserId param.

$apiRequest = new Prepr( '{{YourCustomUserId}}');

or per request

$apiRequest
    ->path('publications/{id}',[
        'id' => 1
    ]),
    ->query([
        'fields' => 'example'
    ])
    ->userId(
        session()->getId() // For Example you can use Laravel's Session ID.
    )
    ->get();

if($apiRequest->getStatusCode() == 200) {

    print_r($apiRequest->getResponse());
}

For more information check the Optimize documentation.

Override the AccessToken in a request

The authorization can also be set for one specific request ->url('url')->authorization('token').

Autopaging

$apiRequest = (new Prepr)
    ->path('publications')
    ->query([
        'limit' => 200 // optional
    ])
    ->autoPaging();

if($apiRequest->getStatusCode() == 200) {
    dump($apiRequest->getResponse());
}

Debug

For debug you can use getRawResponse()