PHP code example of cndrsdrmn / short-url

1. Go to this page and download the library: Download cndrsdrmn/short-url library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

cndrsdrmn / short-url example snippets


return [
    /*
    |--------------------------------------------------------------------------
    | URL Prefix
    |--------------------------------------------------------------------------
    |
    | This value sets the prefix for the short URL routes. It will be used as the
    | base path for accessing the short URLs.
    |
    */
    'prefix' => '/s',

    /*
    |--------------------------------------------------------------------------
    | Allowed Schemas
    |--------------------------------------------------------------------------
    |
    | Specify the schema can be allowed for generated token.
    |
    */
    'allowed_schemas' => [
        'http',
        'https'
    ],

    /*
    |--------------------------------------------------------------------------
    | Database Connection
    |--------------------------------------------------------------------------
    |
    | Specify the database connection to be used for storing short URLs. If null,
    | the default connection defined in the database configuration will be used.
    |
    */
    'connection' => null,

    /*
    |--------------------------------------------------------------------------
    | Middleware
    |--------------------------------------------------------------------------
    |
    | Define the middleware to apply to the short URL routes. By default, the routes
    | are throttled to prevent abuse. You can modify or extend the middleware stack
    | as needed.
    |
    */
    'middleware' => [
        'throttle:100,1',
    ],

    /*
    |--------------------------------------------------------------------------
    | Token Format
    |--------------------------------------------------------------------------
    |
    | Specify the format used to generate tokens for short URLs. Supported values:
    | - "bothify": Mix of letters and numbers (e.g., `A1b2C`)
    | - "numerify": Numbers only (e.g., `12345`)
    | - "lexify": Letters only (e.g., `ABCDE`)
    |
    */
    'token_format' => 'bothify',

    /*
    |--------------------------------------------------------------------------
    | Token Length
    |--------------------------------------------------------------------------
    |
    | Define the length of the token generated for the short URLs. The token's
    | length determines how many characters will be 

use Cndrsdrmn\ShortUrl\Facades\ShortUrl;

$shortUrl = ShortUrl::shorten('https://example.com');
echo $shortUrl; // Outputs the short URL

use Cndrsdrmn\ShortUrl\Facades\ShortUrl;

$builder = ShortUrl::make('https://example.com', [
    'is_single_use' => true,
]);

$shortUrl = $builder->create();
echo $shortUrl->accessLink;

$builder = ShortUrl::make('https://example.com')
    ->headers(['X-Custom-Header' => 'value'])
    ->queries(['utm_source' => 'newsletter']);

$shortUrl = $builder->create();
echo $shortUrl->accessLink;

   ShortUrl::ignoreRoutes();
   

   use App\Http\Controllers\RedirectController;

   Route::get('/s/{token}', [RedirectController::class, 'handle']);
   

ShortUrl::encryptTokenUsing(function ($token) {
    return encrypt($token);
});
bash
php artisan vendor:publish --tag=short-url-config
bash
php artisan test