Starter Kit for PHP

The Starter Kit for PHP provides an ORM-like experience for working with typdy content in your projects.

Starter Kit vs SDK

Typically Headless CMS vendors provide an SDK that consists of a simple API wrapper for accessing your content. This is great for very simple use cases, but it doesn't provide the tools and structure needed to build a robust application.

Our Starter Kits are full featured frameworks in their own right, providing you a plethora of tools and features to help you build your application.

The Starter Kit for PHP delivers an ORM-like experience for working with typdy content. It also bundles a powerful caching layer to help you build fast and scalable applications, without having to worry about the underlying API calls.

Do I Need the Starter kit for PHP?

Short answer: No.

Slightly longer answer: The typdy APIs are fully JSON:API compliant, and can be accessed from any language or framework that can make HTTP requests.

The Starter Kit for PHP is intended to provide a more structured and opinionated way of working with typdy content, and is designed to help you build applications faster and with less boilerplate code.

Starter Kit for PHP Features

The latest version of the Starter Kit for PHP provides the following features:

  • A JSON:API compliant client for accessing your typdy content.
  • An ORM-like experience with models representing your typdy blueprints.
  • Fully relational content reading and editing.
  • Zero configuration caching layer for improved performance.
  • Optional storage drivers for greater application resilience and scalability.
  • Custom webhook handlers for responding to typdy events.
  • Authentication handler with auto-refreshing access tokens.
  • Command line tools for scaffolding models and blueprints.
  • And more!

Getting Started

The full installation and usage instructions for the Starter Kit for PHP can be found in the GitHub repository: typdy/php-starter-kit

Installation

Install the Starter Kit using Composer:

composer require typdy/php-starter-kit

The Starter Kit for PHP requires PHP 8.5 or higher.

Now, it's time to configure the Starter Kit, so that it can connect to your typdy project and work with your file structure:

<?php

use Typdy\StarterKit\Typdy;
use Typdy\StarterKit\TypdyConfig;
use Typdy\StarterKit\Storage\Drivers\RedisDriver;

Typdy::$config = new TypdyConfig(
    team: 'my-team',
    project: 'my-project',

    token: '...',

    privateStoragePath: __DIR__ . '/../storage/private',

    repositoryLocations: [
        __DIR__ . '/../app/Repositories',
    ],

    modelLocations: [
        __DIR__ . '/../app/Models',
    ],

    drivers: [
        RedisDriver::class,
    ],
);

First Steps

Next we need to create a model and a repository for our blueprint. Let's say we have a post blueprint and a latest-posts collection, we can use the scaffold command:

typdy scaffold post --collection="latest-posts"

This will generate a model called Post and a repository called LatestPostsRepository. They will be placed in the app/Models and app/Repositories directories that we specified in our configuration.

Here's the generated model:

<?php

declare(strict_types=1);

namespace App\Models;

use Typdy\StarterKit\Attributes\Blueprint;
use Typdy\StarterKit\Models\Model;

#[Blueprint('post')]
final class Post extends Model
{

}

And the generated repository:

<?php

declare(strict_types=1);

namespace App\Repositories;

use App\Models\Post;
use Typdy\StarterKit\Attributes\Blueprint;
use Typdy\StarterKit\Attributes\Collection;
use Typdy\StarterKit\Models\Attributes\Relationship;
use Typdy\StarterKit\Repositories\Repository;

/**
 * @extends Repository<Post>
 */
#[Collection('latest-posts')] #[Blueprint('post')]
final class LatestPostsRepository extends Repository
{

}

Let's wire up some fields from our post blueprint:

<?php

declare(strict_types=1);

namespace App\Models;

use App\Models\Tag;
use Typdy\StarterKit\Attributes\Blueprint;
use Typdy\StarterKit\Models\Model;

#[Blueprint('post')]
final class Post extends Model
{
    public ?string $title = null;

    public ?string $summary = null;

    public string $metaTitle = '' {
        get {
            return $this->metaTitle ?: $this->title;
        }
    }

    public string $metaDescription = '' {
        get {
            return $this->metaDescription ?: $this->summary;
        }
    }

    public ?string $body = null;

    #[Relationship]
    public ?Media $coverImage = null;

    /**
     * @var array<int, Tag>
     */
    #[Relationship]
    public array $tags = [];
}

Fetching Content

Using the repository, we can now fetch content constructed from our post blueprint. The content will be automatically hydrated into the Post model, and any relationships will be resolved as well:

<?php

use App\Repositories\LatestPostsRepository;

$repo = new LatestPostsRepository();

$posts = $repo->all([
    'parameters' => [
        'include' => 'coverImage,tags',
    ],
]);

foreach ($posts as $post) {

    echo $post->id . PHP_EOL;
    echo $post->identifier . PHP_EOL;

    echo $post->title . PHP_EOL;
    echo $post->summary . PHP_EOL;
    echo $post->metaTitle . PHP_EOL;
    echo $post->metaDescription . PHP_EOL;

    echo $post->created . PHP_EOL;
    echo $post->updated . PHP_EOL;

    if ($post->coverImage) {
        echo $post->coverImage->url . PHP_EOL;
    }

    foreach ($post->tags as $tag) {
        echo $tag->name . PHP_EOL;
    }
}

That's it! Your PHP project is now connected to typdy, and you can start building your application using the Starter Kit for PHP. For more advanced usage and features, please refer to the full documentation on GitHub.

Partnered with some awesome companies

Honeystone Honeystone Piranhamon Piranhamon