title | description |
---|---|
PHP SDK and Laravel |
Getting Started with the OpenFeature PHP SDK on Laravel |
import FlagdContent from '@site/src/components/custom/tutorial/flagd-content.mdx'; import FlagdChangeContent from '@site/src/components/custom/tutorial/flagd-change-content.mdx'; import WhyDefaultContent from '@site/src/components/custom/tutorial/why-default-content.mdx'; import Tabs from '@theme/Tabs'; import TabItem from '@theme/TabItem';
This walk-through teaches you the basics of using OpenFeature with PHP using Laravel.
You'll learn how to:
- Install the OpenFeature PHP SDK
- Install and configure an OpenFeature provider
- Perform basic feature flagging
This walk-through assumes that:
- You should have a good understanding of PHP syntax, data types, operators, functions, and control structures.
- You have PHP 8.0 or later.
- You have Docker installed and running on the host system.
To get started, create a new folder, bootstrap the project, and install the dependencies. This can be done by running the following commands.
composer create-project --prefer-dist laravel/laravel openfeature-php-intro
cd openfeature-php-intro
Define a route for your API by adding the following code to the routes/api.php
file.
Route::get('/hello', function () {
return response()->json(['message' => 'Hello, World!']);
});
Let's install the OpenFeature SDK using the following command.
composer require open-feature/sdk
Update routes/api.php
to import the SDK
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Route;
// diff-add
use OpenFeature\OpenFeatureAPI;
The client can now be used to get a feature flag value. In this case, we'll get a boolean value using the welcome-message flag key.
The second argument is the fallback value, which is returned if there's abnormal behavior.
Route::get('/hello', function () {
// diff-add-block-start
$api = OpenFeatureAPI::getInstance();
$client = $api->getClient();
if($client->getBooleanValue("welcome-message", false)){
return response()->json(['message' => 'Hello, World! open feature']);
}
// diff-add-block-end
return response()->json(['message' => 'Hello, World!']);
});
Let's start the app and see it in action. Run the following command to start the server.
php artisan serve
Open your favorite browser and navigate to http://localhost:8000/api/hello.
If all goes as planned, you should see the message "Hello, World!" in glorious monochrome.
NOTE: You should stop the app by using the keyboard short
ctrl + c
before moving on to the next step.
Also, we have to install PHP flagd provider. Open up your terminal and write this command.
composer require open-feature/flagd-provider
Now let's make the required code changes in our application.
Now, update routes/api.php
to import the flagd provider
.
use OpenFeature\OpenFeatureAPI;
// diff-add-block-start
use OpenFeature\Providers\Flagd\FlagdProvider;
use OpenFeature\Providers\Flagd\config\HttpConfig;
use GuzzleHttp\Client;
use GuzzleHttp\Psr7\HttpFactory;
// diff-add-block-end
Finally, we need to register the provider with OpenFeature.
Route::get('/hello', function () {
$api = OpenFeatureAPI::getInstance();
// diff-add-block-start
$httpClient = new Client();
$httpFactory = new HttpFactory();
$api->setProvider(new FlagdProvider([
'httpConfig' => new HttpConfig(
$httpClient,
$httpFactory,
$httpFactory,
)
]));
// diff-add-block-end
$client = $api->getClient();
if($client->getBooleanValue("welcome-message", false)){
return response()->json(['message' => 'Hello, World! open feature']);
}
return response()->json(['message' => 'Hello, World!']);
});
Now that everything is in place, let's start the app again.
php artisan serve
Open your favorite browser and navigate to http://localhost:8000/api/hello should show the same value as before.
This difference is now the feature flag value can be changed at runtime!
Save the changes to flags.flagd.json
and refresh the browser tab.
This walk-through introduced you to the OpenFeature PHP SDK. It covered how a provider can be configured to perform the flag evaluation and introduced basic feature flagging concepts. It also showcased how feature flags can be updated at runtime, without requiring a redeployment.