Skip to content

Commit cea948f

Browse files
committed
Added Laravel usage to Docs
1 parent 874098c commit cea948f

File tree

3 files changed

+101
-1
lines changed

3 files changed

+101
-1
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ This package provides convenient access to the Cloudflare REST API using PHP.
3030

3131
* PHP >= 8.0
3232
* Minimal API around the [Guzzle HTTP client](https://github.com/guzzle/guzzle)
33+
* Laravel support
3334

3435
## Quick install 🚀
3536

@@ -45,7 +46,7 @@ composer require sergkeim/php-cloudflare-api
4546

4647
* Thanks to [Cloudflare](https://developers.cloudflare.com/api/) for the high quality API and documentation.
4748
* Thanks to [KnpLabs](https://github.com/KnpLabs) for [php-github-api](https://github.com/KnpLabs/php-github-api) used as inspiration for this package.
48-
* Thanks to [Graham Campbell](https://github.com/GrahamCampbell) for [Laravel Manager](https://github.com/GrahamCampbell/Laravel-Manager?tab=readme-ov-file) and [Laravel TestBench](https://github.com/GrahamCampbell/Laravel-TestBench).
49+
* Thanks to [Graham Campbell](https://github.com/GrahamCampbell) for and [Laravel-GitHub](https://github.com/GrahamCampbell/Laravel-GitHub), [Laravel Manager](https://github.com/GrahamCampbell/Laravel-Manager?tab=readme-ov-file) and [Laravel TestBench](https://github.com/GrahamCampbell/Laravel-TestBench).
4950

5051
## License 📎
5152

docs/content/0.index.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ This package provides convenient access to the Cloudflare REST API using PHP.
2929
::list
3030
- PHP >= 8.0
3131
- Minimal API around the [Guzzle HTTP client](https://github.com/guzzle/guzzle)
32+
- Laravel support [see Laravel Usage](/getting-started/laravel)
3233
::
3334

3435
#support
Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
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

Comments
 (0)