Package Data | |
---|---|
Maintainer Username: | rkeppner |
Maintainer Contact: | russell.keppner@gmail.com (Russell Keppner) |
Package Create Date: | 2016-06-16 |
Package Last Update: | 2022-07-19 |
Language: | PHP |
License: | MIT |
Last Refreshed: | 2024-11-22 03:14:50 |
Package Statistics | |
---|---|
Total Downloads: | 13,745 |
Monthly Downloads: | 2 |
Daily Downloads: | 0 |
Total Stars: | 0 |
Total Watchers: | 3 |
Total Forks: | 1 |
Total Open Issues: | 0 |
This package provides a convenient way to retrieve content from remote servers and cache it for use in Laravel.
The assumption is that this process will be used to create a Laravel front-end for a headless CMS setup. However, there are certainly other use cases for which it should work equally well.
Configuration is handled entirely by a single Laravel config file.
Via Composer command line:
$ composer require infusionweb/laravel-remote-content-cache
Or add the package to your composer.json
:
{
"require": {
"infusionweb/laravel-remote-content-cache": "~0.1.0"
}
}
Add the service provider to your config/app.php
:
'providers' => [
//
Kozz\Laravel\Providers\Guzzle::class,
InfusionWeb\Laravel\ContentCache\ContentCacheServiceProvider::class,
];
Add the facade to your config/app.php
:
'aliases' => [
//
'Guzzle' => Kozz\Laravel\Facades\Guzzle::class,
'ContentCache' => InfusionWeb\Laravel\ContentCache\ContentCacheFacade::class,
];
$ php artisan vendor:publish --provider="InfusionWeb\Laravel\ContentCache\ContentCacheServiceProvider"
You may now configure remote API endpoints for consumption and other preferences by editing the config/contentcache.php
file.
Add the command to your app/Console/Kernel.php
:
protected $commands = [
//
\InfusionWeb\Laravel\ContentCache\ContentCacheCommand::class,
];
This enables the console command which can be used to periodically cache the remote content in Laravel:
$ php artisan content:cache
The previous command will cache all configured content types, whereas the following command will cache only the "podcasts" type (assuming it is defined in contentcache.php
).
$ php artisan content:cache podcasts
Assuming that a content type "podcasts" is defined in the config/contentcache.php
file:
<?php
return [
/*
* Default configuration.
*/
'default' => [
// Default length of time (in minutes) to cache content.
'minutes' => 60,
],
/*
* Configuration for custom content filters.
*/
'podcasts' => [
// Length of time (in minutes) to cache content.
'minutes' => 60 * 3, // 3 hours
// REST API endpoint for service from which to retrieve content.
'endpoint' => 'https://podcasts.example.com/api/v1/content/podcasts',
'query' => ['_format' => 'json'],
// Perform data filter (value) on given field name (key). So in this
// case, "id" and "episode" will be cast as integers, and "date_created"
// and "date_changed" will be cast as Carbon date objects. All other
// values will be cast as strings.
'filters' => [
'id' => 'int',
'date_created' => 'date',
'date_changed' => 'date',
'episode' => 'int',
],
// New fields to be created on cached content object from given field names.
// E.g. Given an "episode" value of 13 and a "title" of "Lucky 13", the
// new "slug" attribute (useful for use in routes) will have a value of
// "13-lucky-13".
'fields' => [
'slug' => ['episode', 'title'],
],
// Keys by which the cache should be indexed. I.e. each content
// object will be cached under each of these index keys.
'keys' => [
'id',
'slug',
'uuid',
'episode',
],
],
];
The following code can be used to cache and retrieve podcast content:
<?php
use ContentCache;
// Manually cache remote podcast episodes, if this process is not handled via the
// Artisan console command.
ContentCache::profile('podcasts')->cache();
// Then retrieve a list of all episodes.
$episodes = ContentCache::profile('podcasts')->getAll();
// This could also be shortened to:
$episodes = ContentCache::profile('podcasts')->cache()->getAll();
// Individual content objects can be retrieved via configured indexes.
// In this case, by ID.
$episode = ContentCache::profile('podcasts')->getBy('id', 13);
// Or by slug (from a Laravel Request object), using the "magic function".
$cache = ContentCache::profile('podcasts');
$episode = $cache->getBySlug('13-lucky-13');
// This makes it easy to hand off to a Blade template.
return view('pages.podcast.episode', compact('episode'));
The MIT License (MIT). Please see License File for more information.