To install the Dub PHP SDK, run the following command:
Copy
Ask AI
composer require dub/dub-php
2
Configuration
In your .env file, add your Dub API key:
Copy
Ask AI
DUB_API_KEY=your_api_key
In your config/services.php file, add the following:
Copy
Ask AI
'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
Copy
Ask AI
use Dub\Dub;use Dub\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:
index.php
Copy
Ask AI
$this->app->bind(Dub::class, function ($app) { return Dub::builder()->setSecurity($app['config']->get('services.dub.api_key'))->build();});
You can then inject the authenticated Dub instance throughout your application:
index.php
Copy
Ask AI
use Dub\Laravel\Dub;class LinkController extends Controller { public function createLink(Dub $dub) { // Now you can use the SDK instance $dub->links->create(...); }}
The length of the short link slug. Defaults to 7 if not provided. When used
with prefix, the total length of the key will be prefix.length + keyLength.
The ID of the link in your database. If set, it can be used to identify the
link in future API requests (must be prefixed with ‘ext_’ when passed as a
query parameter). This key is unique across your workspace.
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.