PHP code example of liftric / kirby-cloudfront-invalidations

1. Go to this page and download the library: Download liftric/kirby-cloudfront-invalidations library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

liftric / kirby-cloudfront-invalidations example snippets


'liftric.cloudfrontinvalidations.distributionID' => 'YOUR_CF_DISTRIBUTION_ID'


return [
    'liftric.cloudfrontinvalidations.dependantUrlsForPage' => function ($hook, $page, $oldPage = null) {
        $pages = new Pages();
        foreach ([$page, $oldPage] as $p) {
            if (!$p) continue;
            $pages->add($p);
            $pages->add($p->parents());
            foreach ([$p->related_products(), $p->related_pages()] as $q) {
                if (!$q) continue;
                if ($q->isNotEmpty()) {
                    $pages->add($q->toPages(','));
                }
            }
        }
        $pages->add(new Page(['slug' => 'search']));
        $pages = $pages->toArray(function ($page) {
            return array($page->urlForLanguage('de'), $page->urlForLanguage('en'));
        });
        $pages = array_values($pages);
        $pages = array_merge(...$pages);
        return $pages;
    },
    'hooks' => [
        'route:after' => function ($result, $path) {
            if (Str::startsWith($path, 'api/')) {
                header('Cache-Control: private, no-cache, no-store');
            }
        }
    ]
];