1
0
mirror of https://github.com/Unleash/unleash.git synced 2024-12-28 00:06:53 +01:00
unleash.unleash/website/docs/reference/sdks/php.md

101 lines
3.8 KiB
Markdown
Raw Normal View History

2021-08-19 13:34:05 +02:00
---
title: PHP SDK
---
In this guide we explain how to use feature toggles in a PHP application using Unleash-hosted. We will be using the open source Unleash [PHP Client SDK](https://github.com/Unleash/unleash-client-php).
refactor: move docs into new structure / fix links for SEO (#2416) ## What This (admittedly massive) PR updates the "physical" documentation structure and fixes url inconsistencies and SEO problems reported by marketing. The main points are: - remove or move directories : advanced, user_guide, deploy, api - move the files contained within to the appropriate one of topics, how-to, tutorials, or reference - update internal doc links and product links to the content - create client-side redirects for all the urls that have changed. A number of the files have been renamed in small ways to better match their url and to make them easier to find. Additionally, the top-level api directory has been moved to /reference/api/legacy/unleash (see the discussion points section for more on this). ## Why When moving our doc structure to diataxis a while back, we left the "physical' files lying where they were, because it didn't matter much to the new structure. However, that did introduce some inconsistencies with where you place docs and how we organize them. There's also the discrepancies in whether urls us underscores or hyphens (which isn't necessarily the same as their file name), which has been annoying me for a while, but now has also been raised by marketing as an issue in terms of SEO. ## Discussion points The old, hand-written API docs have been moved from /api to /reference/api/legacy/unleash. There _is_ a /reference/api/unleash directory, but this is being populated by the OpenAPI plugin, and mixing those could only cause trouble. However, I'm unsure about putting /legacy/ in the title, because the API isn't legacy, the docs are. Maybe we could use another path? Like /old-docs/ or something? I'd appreciate some input on this.
2022-11-22 10:05:30 +01:00
> You will need your `API URL` and your `API token` in order to connect the Client SDK to you Unleash instance. You can find this information in the “Admin” section Unleash management UI. [Read more](../../how-to/how-to-create-api-tokens)
2021-08-19 13:34:05 +02:00
## Step 1: Install the client SDK {#step-1-install-the-client-sdk}
First we must add Unleash Client SDK as a dependency to your project. Below is an example of how to install it via composer:
```shell
composer require unleash/client guzzlehttp/guzzle symfony/cache
```
> Note: You can install any other PSR-16, PSR-17 and PSR-18 implementations instead of guzzlehttp/guzzle and symfony/cache
## Step 2: Create a new Unleash Instance {#step-2-create-a-new-unleash-instance}
Next we must initialize a new instance of the Unleash Client.
```php
<?php
use Unleash\Client\UnleashBuilder;
$unleash = UnleashBuilder::create()
->withAppName('my.php-app')
->withInstanceId('your-instance-1')
->withAppUrl('API URL')
->withHeader('Authorization', 'API token')
->build();
```
In your app you typically just want one instance of Unleash, and inject that where you need it. You will typically use a dependency injection frameworks to manage this.
You should change the URL and the Authorization header (API token) with the correct values for your instance, which you may locate under “Instance admin” in the menu.
## Step 3: Use the feature toggle {#step-3-use-the-feature-toggle}
Now that we have initialized the client SDK we can start using feature toggles defined in Unleash in our application. To achieve this we have the “isEnabled” method available, which will allow us to check the value of a feature toggle. This method will return **true** or **false** based on whether the feature should be enabled or disabled for the current request.
```php
<?php
if ($unleash->isEnabled("AwesomeFeature")) {
//do some magic
} else {
//do old boring stuff
}
```
Read more about the [Unleash architecture](https://www.unleash-hosted.com/articles/our-unique-architecture) to learn how it works in more details
## Step 4: Provide Unleash Context {#step-4-provide-unleash-context}
refactor: move docs into new structure / fix links for SEO (#2416) ## What This (admittedly massive) PR updates the "physical" documentation structure and fixes url inconsistencies and SEO problems reported by marketing. The main points are: - remove or move directories : advanced, user_guide, deploy, api - move the files contained within to the appropriate one of topics, how-to, tutorials, or reference - update internal doc links and product links to the content - create client-side redirects for all the urls that have changed. A number of the files have been renamed in small ways to better match their url and to make them easier to find. Additionally, the top-level api directory has been moved to /reference/api/legacy/unleash (see the discussion points section for more on this). ## Why When moving our doc structure to diataxis a while back, we left the "physical' files lying where they were, because it didn't matter much to the new structure. However, that did introduce some inconsistencies with where you place docs and how we organize them. There's also the discrepancies in whether urls us underscores or hyphens (which isn't necessarily the same as their file name), which has been annoying me for a while, but now has also been raised by marketing as an issue in terms of SEO. ## Discussion points The old, hand-written API docs have been moved from /api to /reference/api/legacy/unleash. There _is_ a /reference/api/unleash directory, but this is being populated by the OpenAPI plugin, and mixing those could only cause trouble. However, I'm unsure about putting /legacy/ in the title, because the API isn't legacy, the docs are. Maybe we could use another path? Like /old-docs/ or something? I'd appreciate some input on this.
2022-11-22 10:05:30 +01:00
It is the client SDK that computes whether a feature toggle should be considered enabled or disabled for specific use request. This is the job of the [activation strategies](../../reference/activation-strategies.md), which are implemented in the client SDK.
2021-08-19 13:34:05 +02:00
The activation strategies is an implementation of rules based on data, which you provide as part of the Unleash Context.
**a) As argument to the isEnabled call**
The simplest way to provide the Unleash Context is as part of the “isEnabled” call:
```php
<?php
use Unleash\Client\Configuration\UnleashContext;
$context = new UnleashContext(
currentUserId: 'user@mail.com',
);
$unleash->isEnabled("someToggle", $context);
```
**b) Via a UnleashContextProvider**
This is a bit more advanced approach, where you configure a unleash-context provider. By doing this you do not have to rebuild or to pass the unleash-context object to every place you are calling `$unleash->isEnabled()`.
```php
<?php
use Unleash\Client\UnleashBuilder;
$contextProvider = new MyAwesomeContextProvider();
$unleash = UnleashBuilder::create()
->withAppName('my.php-app')
->withInstanceId('your-instance-1')
docs: Remove/update references to Heroku (#2099) ## What This PR removes or updates references in the docs to Heroku. Most of the code samples have been replaced with a more generic `unleash.example.com` url, while other references have been removed or updated. Also removes old OpenAPI files that are out of date and redundant with the new generation. ## Background Come November and Heroku will no longer offer free deployments of Unleash, so it's about time we remove that claim. Links to the heroku instance are also outdated because we don't have that instance running anymore. Finally, the OpenAPI files we do have there are old and static, so they don't match the current reality. ## Commits * Meta: update ignore file to ignore autogenerated docs I must've missed the ignore file when looking for patterns. * docs: delete old openapi file. This seems to have been a holdover from 2020 and is probably hand-written. It has been superseded by the new autogenerated OpenAPI docs. * docs: add notes for heroku changes to the frontend readme and pkg * docs: remove old openapi article and add redirects to new openapi * docs: fix link in overview doc: point to GitHub instead of heroku * docs: update quickstart docs with new heroku details * docs: remove reference to crashing heroku instance * docs: remove references to herokuapp in code samples * docs: add a placeholder comment * docs: update references for heroku updates * docs: keep using unleash4 for enterprise * docs: remove start:heroku script in favor of start:sandbox * docs: remove 'deploy on heroku button' Now that it's not free anymore (or won't be very shortly), let's remove it. * docs: remove extra newline
2022-10-19 14:02:00 +02:00
->withAppUrl('https://unleash.example.com/api/')
->withContextProvider($contextProvider)
->build();
// Anywhere in the code unleash will get the unleash context from your registered provider.
$unleash->isEnabled("someToggle");
```
2021-08-19 13:34:05 +02:00
> You can read more complete documentation in the [Client SDK repository](https://github.com/Unleash/unleash-client-php).