|
| 1 | +--- |
| 2 | +title: Laravel |
| 3 | +--- |
| 4 | + |
| 5 | +# Laravel |
| 6 | + |
| 7 | +Once installed, if you are not using automatic package discovery, then you need to register the `Cloudflare\CloudflareServiceProvider` service provider in your `config/app.php`. |
| 8 | + |
| 9 | +You can also optionally alias our facade: |
| 10 | + |
| 11 | +```php [php] |
| 12 | +'Cloudflare' => Cloudflare\Facades\Cloudflare::class, |
| 13 | +``` |
| 14 | + |
| 15 | +## Configuration |
| 16 | + |
| 17 | +Laravel Cloudflare requires connection configuration. |
| 18 | + |
| 19 | +To get started, you'll need to publish all vendor assets: |
| 20 | + |
| 21 | +```bash [bash] |
| 22 | +php artisan vendor:publish |
| 23 | +``` |
| 24 | + |
| 25 | +This will create a `config/cloudflare.php` file in your app that you can modify to set your configuration. Also, make sure you check for changes to the original config file in this package between releases. |
| 26 | + |
| 27 | +There are two config options: |
| 28 | + |
| 29 | +##### Default Connection Name |
| 30 | + |
| 31 | +This option (`'default'`) is where you may specify which of the connections below you wish to use as your default connection for all work. Of course, you may use many connections at once using the manager class. The default value for this setting is `'main'`. |
| 32 | + |
| 33 | +##### Cloudflare Connections |
| 34 | + |
| 35 | +This option (`'connections'`) is where each of the connections are setup for your application. Example configuration has been included, but you may add as many connections as you would like.. |
| 36 | + |
| 37 | +## Usage |
| 38 | + |
| 39 | +##### CloudflareManager |
| 40 | + |
| 41 | +`CloudflareManager` is bound to the ioc container as `'cloudflare'` and can be accessed using the `Facades\Cloudflare` facade. |
| 42 | + |
| 43 | +This class implements the `ManagerInterface` by extending `AbstractManager`. The interface and abstract class are both part of [Graham Campbell - Laravel Manager](https://github.com/GrahamCampbell/Laravel-Manager) package, so you may want to go and checkout the docs for how to use the manager class over at [that repo](https://github.com/GrahamCampbell/Laravel-Manager#usage). |
| 44 | + |
| 45 | +**Note**: that the connection class returned will always be an instance of `Cloudflare\Client`. |
| 46 | + |
| 47 | +##### Facades\Cloudflare |
| 48 | + |
| 49 | +This facade will dynamically pass static method calls to the `'cloudflare'` object in the ioc container which by default is the `CloudflareManager` class. |
| 50 | + |
| 51 | +##### CloudflareServiceProvider |
| 52 | + |
| 53 | +This class contains no public methods of interest. This class should be added to the providers array in `config/app.php`. This class will setup ioc bindings. |
| 54 | + |
| 55 | +Here you can see an example of just how simple this package is to use. Out of the box, the default adapter is `main`. After you enter your `token` in the config file, it will just work: |
| 56 | + |
| 57 | +```php [php] |
| 58 | +use Cloudflare\Facades\Cloudflare; |
| 59 | +// you can alias this in config/app.php if you like |
| 60 | + |
| 61 | +Cloudflare::accounts()->list(); |
| 62 | + |
| 63 | +// or |
| 64 | + |
| 65 | +Cloudflare::accounts()->details('ACCOUNT_ID'); |
| 66 | +``` |
| 67 | + |
| 68 | +The cloudflare manager will behave like it is a `Cloudflare\Client` class. If you want to call specific connections, you can do with the `connection` method: |
| 69 | + |
| 70 | +```php [php] |
| 71 | +use Cloudflare\Facades\Cloudflare; |
| 72 | + |
| 73 | +// the secondary connection is the other example provided in the default config |
| 74 | +Cloudflare::connection('secondary')->accounts()->members()->details('ACCOUNT_ID', 'MEMBER_ID'); |
| 75 | +``` |
| 76 | + |
| 77 | +If you prefer to use dependency injection over facades, then you can easily inject the manager like so: |
| 78 | + |
| 79 | +```php [php] |
| 80 | +use Cloudflare\CloudflareManager; |
| 81 | + |
| 82 | +class Foo |
| 83 | +{ |
| 84 | + |
| 85 | + public function __construct( |
| 86 | + private CloudflareManager $cloudflare |
| 87 | + ) { |
| 88 | + |
| 89 | + } |
| 90 | + |
| 91 | + public function bar() |
| 92 | + { |
| 93 | + $this->cloudflare->accounts()->details('ACCOUNT_ID'); |
| 94 | + } |
| 95 | +} |
| 96 | + |
| 97 | +app(Foo::class)->bar(); |
| 98 | +``` |
0 commit comments