|
1 | | -# statamic-cloudflare |
| 1 | +[](LICENSE.md) |
| 2 | + |
| 3 | +Cloudflare integration for Statamic with CLI, static caching integration and control panel integration. |
| 4 | + |
| 5 | +Inspired by [sebdesign/artisan-cloudflare](https://github.com/sebdesign/artisan-cloudflare) |
| 6 | + |
| 7 | +## Requirements |
| 8 | + |
| 9 | +* Statamic 3.1.2 or higher |
| 10 | + |
| 11 | +## Installation |
| 12 | + |
| 13 | +You can install the package via composer: |
| 14 | + |
| 15 | +```shell |
| 16 | +composer require handmadeweb/statamic-cloudflare |
| 17 | +``` |
| 18 | + |
| 19 | +#### Copy the package config to your local config with the publish command: |
| 20 | + |
| 21 | +```shell |
| 22 | +php artisan vendor:publish --provider="HandmadeWeb\StatamicCloudflare\ServiceProvider" |
| 23 | +``` |
| 24 | + |
| 25 | +Then add your cloudflare details to your `.env` file. |
| 26 | + |
| 27 | +```env |
| 28 | +CLOUDFLARE_KEY= |
| 29 | +CLOUDFLARE_EMAIL= |
| 30 | +``` |
| 31 | + |
| 32 | +And configure your `zones` in your `statamic-cloudflare.php` |
| 33 | + |
| 34 | +```php |
| 35 | + /* |
| 36 | + * Array of zones. |
| 37 | + * |
| 38 | + * Each zone must have its domain as the key. The value should be your zoneId. |
| 39 | + * |
| 40 | + * you can find your zoneId under 'Account Home > site > Api'. |
| 41 | + * |
| 42 | + * E.g. |
| 43 | + * |
| 44 | + * 'example.com' => '023e105f4ecef8ad9ca31a8372d0c353' |
| 45 | + */ |
| 46 | + 'zones' => [ |
| 47 | + // |
| 48 | + ], |
| 49 | +``` |
| 50 | + |
| 51 | +By default all purge actions `except the Cli commands` will be queued with your default queue. |
| 52 | + |
| 53 | +This can be changed in the configuration. |
| 54 | + |
| 55 | +```php |
| 56 | + /* |
| 57 | + * Should purges be processed in a queue? |
| 58 | + * CLI commands will always run on request. |
| 59 | + */ |
| 60 | + 'queued' => true, |
| 61 | +``` |
| 62 | + |
| 63 | +## Usage |
| 64 | + |
| 65 | +### Cli |
| 66 | + |
| 67 | +You can purge everything via the following commands. |
| 68 | + |
| 69 | +```shell |
| 70 | +php artisan cloudflare:cache:purge:everything |
| 71 | +``` |
| 72 | + |
| 73 | +```shell |
| 74 | +php please cloudflare:cache:purge:everything |
| 75 | +``` |
| 76 | + |
| 77 | +### Static Caching |
| 78 | + |
| 79 | +If you want to use `statamic-cloudflare` as a static cache strategy then you will need to manually register the cacher in the register method of your `App\Providers\AppServiceProvider` class. |
| 80 | + |
| 81 | +```php |
| 82 | +/** |
| 83 | + * Register any application services. |
| 84 | + * |
| 85 | + * @return void |
| 86 | + */ |
| 87 | +public function register() |
| 88 | +{ |
| 89 | + \HandmadeWeb\StatamicCloudflare\Cloudflare::registerCacher(); |
| 90 | +} |
| 91 | +``` |
| 92 | + |
| 93 | +Then update your `static_cache` config. |
| 94 | + |
| 95 | +```php |
| 96 | +'strategies' => [ |
| 97 | + |
| 98 | + 'half' => [ |
| 99 | + 'driver' => 'application', |
| 100 | + 'expiry' => null, |
| 101 | + ], |
| 102 | + |
| 103 | + 'full' => [ |
| 104 | + 'driver' => 'file', |
| 105 | + 'path' => public_path('static'), |
| 106 | + 'lock_hold_length' => 0, |
| 107 | + ], |
| 108 | + |
| 109 | + 'cloudflare' => [ |
| 110 | + 'driver' => 'cloudflare', |
| 111 | + 'strategy' => 'null', |
| 112 | + ], |
| 113 | +], |
| 114 | +``` |
| 115 | + |
| 116 | +Then update the `static_cache` strategy at the top of the configuration to: |
| 117 | + |
| 118 | +```php |
| 119 | +'strategy' => 'cloudflare', |
| 120 | +``` |
| 121 | + |
| 122 | +Currently the Cloudflare integration is only used for purging, If you would like to use another caching strategy in combination with this caching strategy, then you are free to do that. |
| 123 | + |
| 124 | +This can be done by updating the `strategy` section within the `cloudflare` strategy, below is an example where we will be caching the application using the [half measure](https://statamic.dev/static-caching#application-driver) |
| 125 | + |
| 126 | +```php |
| 127 | +'cloudflare' => [ |
| 128 | + 'driver' => 'cloudflare', |
| 129 | + 'strategy' => 'half', |
| 130 | +], |
| 131 | +``` |
| 132 | + |
| 133 | +In theory, you should be able to use any caching strategy here, such as the [full measure](https://statamic.dev/static-caching#file-driver) or any other first or third party strategies, `statamic-cloudfare` will simply pass requests to the defined strategy and will just hook into the purge actions to also purge the page in Cloudflare. |
| 134 | + |
| 135 | +### Control Panel |
| 136 | + |
| 137 | +`statamic-cloudflare` will add a utility to your Statamic CP. |
| 138 | + |
| 139 | +route: `/cp/utilities/cloudflare` |
| 140 | + |
| 141 | +This will be available to `Super Users` and Users who have the ` |
| 142 | +Cloudflare Manager` permission. |
| 143 | + |
| 144 | +You can purge everything quickly from here. |
| 145 | + |
| 146 | +## Changelog |
| 147 | + |
| 148 | +Please see [CHANGELOG](https://github.com/handmadeweb/statamic-cloudflare/blob/main/CHANGELOG.md) for more information what has changed recently. |
| 149 | + |
| 150 | +## Contributing |
| 151 | + |
| 152 | +Please see [CONTRIBUTING](https://github.com/handmadeweb/statamic-cloudflare/blob/main/CONTRIBUTING.md) for details. |
| 153 | + |
| 154 | +## Credits |
| 155 | + |
| 156 | +- [Handmade Web & Design](https://github.com/handmadeweb) |
| 157 | +- [Michael Rook](https://github.com/michaelr0) |
| 158 | +- [All Contributors](https://github.com/handmadeweb/statamic-cloudflare/graphs/contributors) |
| 159 | + |
| 160 | +## License |
| 161 | + |
| 162 | +The MIT License (MIT). Please see [License File](https://github.com/handmadeweb/statamic-cloudflare/blob/main/LICENSE.md) for more information. |
0 commit comments