Skip to content

Commit

Permalink
Show how to create the short URL builder using the service container.
Browse files Browse the repository at this point in the history
  • Loading branch information
ash-jc-allen committed Apr 26, 2024
1 parent 035d322 commit 5aadd20
Showing 1 changed file with 55 additions and 49 deletions.
104 changes: 55 additions & 49 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -102,14 +102,18 @@ php artisan migrate
```

## Usage

### Building Shortened URLs

#### Quick Start
The quickest way to get started with creating a shortened URL is by using the snippet below. The ``` ->make() ``` method

The quickest way to get started with creating a shortened URL is by using the snippet below. The `->make()` method
returns a ShortURL model that you can grab the shortened URL from.

```php
$builder = new \AshAllenDesign\ShortURL\Classes\Builder();
use AshAllenDesign\ShortURL\Classes\Builder;

$shortURLObject = $builder->destinationUrl('https://destination.com')->make();
$shortURLObject = app(Builder::class)->destinationUrl('https://destination.com')->make();
$shortURL = $shortURLObject->default_short_url;
```

Expand All @@ -122,9 +126,9 @@ You may wish to define a custom key yourself for that URL that is more meaningfu
do this by using the ``` ->urlKey() ``` method. Example:

```php
$builder = new \AshAllenDesign\ShortURL\Classes\Builder();
use AshAllenDesign\ShortURL\Classes\Builder;

$shortURLObject = $builder->destinationUrl('https://destination.com')->urlKey('custom-key')->make();
$shortURLObject = app(Builder::class)->destinationUrl('https://destination.com')->urlKey('custom-key')->make();
$shortURL = $shortURLObject->default_short_url;

// Short URL: https://webapp.com/short/custom-key
Expand All @@ -149,17 +153,19 @@ If you want to override whether if tracking is enabled or not when creating a sh
This method accepts a boolean but defaults to ``` true ``` if a parameter is not passed.

The example below shows how to enable tracking for the URL and override the config variable:

```php
$builder = new \AshAllenDesign\ShortURL\Classes\Builder();
use AshAllenDesign\ShortURL\Classes\Builder;

$shortURLObject = $builder->destinationUrl('https://destination.com')->trackVisits()->make();
$shortURLObject = app(Builder::class)->destinationUrl('https://destination.com')->trackVisits()->make();
```

The example below shows how to disable tracking for the URL and override the default config variable:

```php
$builder = new \AshAllenDesign\ShortURL\Classes\Builder();
use AshAllenDesign\ShortURL\Classes\Builder;

$shortURLObject = $builder->destinationUrl('https://destination.com')->trackVisits(false)->make();
$shortURLObject = app(Builder::class)->destinationUrl('https://destination.com')->trackVisits(false)->make();
```

##### Tracking IP Address
Expand All @@ -169,9 +175,9 @@ If you want to override whether if IP address tracking is enabled or not when cr

The example below shows how to enable IP address tracking for the URL and override the default config variable:
```php
$builder = new \AshAllenDesign\ShortURL\Classes\Builder();
use AshAllenDesign\ShortURL\Classes\Builder;

$shortURLObject = $builder->destinationUrl('https://destination.com')->trackVisits()->trackIPAddress()->make();
$shortURLObject = app(Builder::class)->destinationUrl('https://destination.com')->trackVisits()->trackIPAddress()->make();
```

##### Tracking Browser & Browser Version
Expand All @@ -182,16 +188,16 @@ but defaults to ``` true ``` if a parameter is not passed.

The example below shows how to enable browser name tracking for the URL and override the default config variable:
```php
$builder = new \AshAllenDesign\ShortURL\Classes\Builder();
use AshAllenDesign\ShortURL\Classes\Builder;

$shortURLObject = $builder->destinationUrl('https://destination.com')->trackVisits()->trackBrowser()->make();
$shortURLObject = app(Builder::class)->destinationUrl('https://destination.com')->trackVisits()->trackBrowser()->make();
```

The example below shows how to enable browser version tracking for the URL and override the default config variable:
```php
$builder = new \AshAllenDesign\ShortURL\Classes\Builder();
use AshAllenDesign\ShortURL\Classes\Builder;

$shortURLObject = $builder->destinationUrl('https://destination.com')->trackVisits()->trackBrowserVersion()->make();
$shortURLObject = app(Builder::class)->destinationUrl('https://destination.com')->trackVisits()->trackBrowserVersion()->make();
```

##### Tracking Operating System & Operating System Version
Expand All @@ -202,16 +208,16 @@ methods. These methods accept a boolean but default to ``` true ``` if a paramet

The example below shows how to enable operating system name tracking for the URL and override the default config variable:
```php
$builder = new \AshAllenDesign\ShortURL\Classes\Builder();
use AshAllenDesign\ShortURL\Classes\Builder;

$shortURLObject = $builder->destinationUrl('https://destination.com')->trackVisits()->trackOperatingSystem()->make();
$shortURLObject = app(Builder::class)->destinationUrl('https://destination.com')->trackVisits()->trackOperatingSystem()->make();
```

The example below shows how to enable operating system version tracking for the URL and override the default config variable:
```php
$builder = new \AshAllenDesign\ShortURL\Classes\Builder();
use AshAllenDesign\ShortURL\Classes\Builder;

$shortURLObject = $builder->destinationUrl('https://destination.com')->trackVisits()->trackOperatingSystemVersion()->make();
$shortURLObject = app(Builder::class)->destinationUrl('https://destination.com')->trackVisits()->trackOperatingSystemVersion()->make();
```

##### Tracking Device Type
Expand All @@ -221,9 +227,9 @@ If you want to override whether if device type tracking is enabled or not when c

The example below shows how to enable device type tracking for the URL and override the default config variable:
```php
$builder = new \AshAllenDesign\ShortURL\Classes\Builder();
use AshAllenDesign\ShortURL\Classes\Builder;

$shortURLObject = $builder->destinationUrl('https://destination.com')->trackVisits()->trackDeviceType()->make();
$shortURLObject = app(Builder::class)->destinationUrl('https://destination.com')->trackVisits()->trackDeviceType()->make();
```

##### Tracking Referer URL
Expand All @@ -233,9 +239,9 @@ If you want to override whether if referer URL tracking is enabled or not when c

The example below shows how to enable referer URL tracking for the URL and override the default config variable:
```php
$builder = new \AshAllenDesign\ShortURL\Classes\Builder();
use AshAllenDesign\ShortURL\Classes\Builder;

$shortURLObject = $builder->destinationUrl('https://destination.com')->trackVisits()->trackRefererURL()->make();
$shortURLObject = app(Builder::class)->destinationUrl('https://destination.com')->trackVisits()->trackRefererURL()->make();
```

#### Custom Short URL Fields
Expand Down Expand Up @@ -270,9 +276,9 @@ To create a single use shortened URL, you can use the ``` ->singleUse() ``` meth

The example below shows how to create a single use shortened URL:
```php
$builder = new \AshAllenDesign\ShortURL\Classes\Builder();
$shortURLObject = $builder->destinationUrl('https://destination.com')->singleUse()->make();
use AshAllenDesign\ShortURL\Classes\Builder;

$shortURLObject = app(Builder::class)->destinationUrl('https://destination.com')->singleUse()->make();
```

#### Enforce HTTPS
Expand All @@ -284,9 +290,9 @@ To enforce HTTPS, you can use the ``` ->secure() ``` method when building the sh

The example below shows how to create a secure shortened URL:
```php
$builder = new \AshAllenDesign\ShortURL\Classes\Builder();
$shortURLObject = $builder->destinationUrl('http://destination.com')->secure()->make();
use AshAllenDesign\ShortURL\Classes\Builder;

$shortURLObject = app(Builder::class)->destinationUrl('http://destination.com')->secure()->make();

// Destination URL: https://destination.com
```
Expand All @@ -297,9 +303,9 @@ When building a short URL, you might want to forward the query parameters sent i
Alternatively, you can also use the `->forwardQueryParams()` method when building your shortened URL, as shown in the example below:

```php
$builder = new \AshAllenDesign\ShortURL\Classes\Builder();
$shortURLObject = $builder->destinationUrl('http://destination.com?param1=test')->forwardQueryParams()->make();
use AshAllenDesign\ShortURL\Classes\Builder;

$shortURLObject = app(Builder::class)->destinationUrl('http://destination.com?param1=test')->forwardQueryParams()->make();
```

Based on the example above, assuming that the original short URL's `destination_url` was `https://destination.com`, making a request to `https://webapp.com/short/xxx?param1=abc&param2=def` would redirect to `https://destination.com?param1=test&param2=def`
Expand All @@ -310,11 +316,12 @@ By default, all short URLs are redirected with a ``` 301 ``` HTTP status code. B
the shortened URL using the ``` ->redirectStatusCode() ``` method.

The example below shows how to create a shortened URL with a redirect HTTP status code of ``` 302 ```:
```php
$builder = new \AshAllenDesign\ShortURL\Classes\Builder();

$shortURLObject = $builder->destinationUrl('http://destination.com')->redirectStatusCode(302)->make();
```

```php
use AshAllenDesign\ShortURL\Classes\Builder;

$shortURLObject = app(Builder::class)->destinationUrl('http://destination.com')->redirectStatusCode(302)->make();
```

#### Activation and Deactivation Times

Expand All @@ -327,18 +334,18 @@ a given date and then automatically deactivate that URL when the marketing campa
The example below shows how to create a shortened URL that will be active from this time tomorrow onwards:

```php
$builder = new \AshAllenDesign\ShortURL\Classes\Builder();
$shortURLObject = $builder->activateAt(\Carbon\Carbon::now()->addDay())->make();
use AshAllenDesign\ShortURL\Classes\Builder;

$shortURLObject = app(Builder::class)->activateAt(\Carbon\Carbon::now()->addDay())->make();
```

The example below shows how to create a shortened URL that will be active from this time tomorrow onwards and then is
deactivated the day after:

```php
$builder = new \AshAllenDesign\ShortURL\Classes\Builder();
$shortURLObject = $builder->activateAt(\Carbon\Carbon::now()->addDay())
use AshAllenDesign\ShortURL\Classes\Builder;

$shortURLObject = app(Builder::class)->activateAt(\Carbon\Carbon::now()->addDay())
->deactivateAt(\Carbon\Carbon::now()->addDays(2))
->make();
```
Expand All @@ -348,9 +355,9 @@ $shortURLObject = $builder->activateAt(\Carbon\Carbon::now()->addDay())
By default, the package will use the ID of the last inserted short URL as the seed for generating a short URL's key. In some cases, you may want to use a custom seed instead. To do this, you can pass an integer to the `generateKeyUsing` method like so:

```php
$builder = new \AshAllenDesign\ShortURL\Classes\Builder();
$shortURLObject = $builder->destinationUrl('https://destination.com')
use AshAllenDesign\ShortURL\Classes\Builder;

$shortURLObject = app(Builder::class)->destinationUrl('https://destination.com')
->generateKeyUsing(12345)
->make();
```
Expand Down Expand Up @@ -387,8 +394,7 @@ For example, let's take this block of code that uses `if` when building the shor
```php
use AshAllenDesign\ShortURL\Classes\Builder;

$shortURLObject = (new Builder())
->destinationUrl('https://destination.com');
$builder = app(Builder::class)->destinationUrl('https://destination.com');

if ($request->date('activation')) {
$builder = $builder->activateAt($request->date('activation'));
Expand All @@ -403,7 +409,7 @@ This could be rewritten using `when` like so:
use AshAllenDesign\ShortURL\Classes\Builder;
use Carbon\Carbon;

$shortURLObject = (new Builder())
$shortURLObject = app(Builder::class)
->destinationUrl('https://destination.com')
->when(
$request->date('activation'),
Expand Down

0 comments on commit 5aadd20

Please sign in to comment.