To install the Dub PHP SDK, run the following command:
composer require dub/dub-php
2
Configuration
In your .env file, add your Dub API key:
DUB_API_KEY=your_api_key
In your config/services.php file, add the following:
'dub'=>['api_key'=>env('DUB_API_KEY'),],
3
Initialize
You can now create an instance of the Dub class and pass in your API key:
index.php
useDub\Dub;useDub\Components\Security;$dub=Dub::builder()->setSecurity(config('services.dub.api_key'))->build();// create a link$dub->links->create(...);
4
Service Container (Optional)
If you want to be able to inject the Dub class via the service container, add this to the register method of your AppServiceProvider.php:
You can then inject the authenticated Dub instance throughout your application:
index.php
useDub\Laravel\Dub;classLinkControllerextendsController{publicfunctioncreateLink(Dub$dub){// Now you can use the SDK instance$dub->links->create(...);}}
The prefix of the short link slug for randomly-generated keys (e.g. if prefix
is /c/, generated keys will be in the /c/:key format). Will be ignored if
key is provided.
The identifier of the short link that is unique across your workspace. If set,
it can be used to identify your short link for client-side click tracking.