PHP Laravel
Install PHP Laravel SDK
You can find the latest version on our github Repository:
https://github.com/switchover-io/laravel-integration
Install our Switchover Laravel SDK ina aexisting Laravel App using composer:
composer require switchover/laravel-integration
Add your SDK Key.
You will find your SDK Key on the environment page. Copy it and add it in the .env to initialize the client:
SWITCHOVER_SDK_KEY=<YOUR SDK KEY>
and configure the caching time:
SWITCHOVER_CACHE_TIME=10 # Warning: 0 will cache forever
After that you can use Switchover on any place using:
use SwitchoverLaravel\Switchover;
use Switchover\Context;
Basic usage example
Basic usage:
class HomeController extends Controller
{
public function index() {
$userCtx = new Context([
'email' => Auth::user()->email;
])
;
$coolNewFeatures = Switchover::toggleValue('cool-feature', false, $userCtx);
return //...
}
}
You can also publish the underlying config file to get full control over the config e.g. for the Guzzle Http client:
php artisan vendor:publish --tag="switchover-config"
After that, you will find the configuration in your app/config folder
Blade directive
Of course we also offer an extension to use Feature Toggles directly in Blade Templates. There are two ways to use them:
name | description |
@hasFeature | use it to check boolean flags in your blade tempate |
@hasConditionalFeature | use it if you have also conditional values configured |
both directives ends on @endHasFeature
Example:
<body class="myawesomeClass">
@hasFeature("yourToggleName")
Your content will be shown
@endHasFeature
</body>
<body class="myawesomeClass">
@hasConditionalFeature("yourToggleName",["email" => "wilhem@switch-over.io"])
Your content will be shown
@endHasFeature
</body>
Options
Option | Value |
---|---|
cache.time | Sets time in seconds before the internal cache becomes stale and will be refreshed (TTL). Default is 60 seconds. The value 0 will keep the cache forever. |
http | The client uses guzzlehttp/guzzle for http requests. You can pass an array of options to the Guzzle Http Client. |
Context Object
To check conditions in the client sdk a context object must be created and passed to the toggleValue method.
$contextArray = array("key" => "value");
$context = new Context($contextArray);
Public methods
toggleValue()
Parameters | Description |
name | the name of the feature toggle configured in switchover |
defaultValue | It will return the given default value: * – When toggle is INACTIVE * – When evalution fails * – When client was not fully initialized |
context | Context Object (array) |
Switchover::toggleValue(
string $name,
boolean $defaultValue,
Context $context = null
)
refresh()
Manually refreshes toggles.
Switchover::refresh();
getToggleKeys()
Get all toggle keys, which currently loaded
Switchover::getToggleKeys();