Custom URL Generator
The URL generator controls AssetConnect route URLs for protected assets and temporary URLs. Public storage disks normally do not use these routes; their URLs are generated by the configured storage disk through public_url or Flysystem public URL support.
Use a custom URL generator when you want a different route shape for protected or temporary asset links while keeping AssetConnect's authorization and temporary-token handling.
Interface
Custom URL generators must implement UrlGeneratorInterface:
use CodeIgniter\Router\RouteCollection;
use Maniaba\AssetConnect\Asset\Asset;
use Maniaba\AssetConnect\AssetVariants\AssetVariant;
interface UrlGeneratorInterface
{
public static function routes(RouteCollection &$routes): void;
public static function params(Asset $asset, ?AssetVariant $variant, ?string $token = null): array;
}
The params() array must contain the route names AssetConnect asks for:
asset-connect.showasset-connect.show_variantasset-connect.temporaryasset-connect.temporary_variant
Example
<?php
declare(strict_types=1);
namespace App\UrlGenerators;
use CodeIgniter\Router\RouteCollection;
use Maniaba\AssetConnect\Asset\Asset;
use Maniaba\AssetConnect\AssetVariants\AssetVariant;
use Maniaba\AssetConnect\Controllers\AssetConnectController;
use Maniaba\AssetConnect\UrlGenerator\Interfaces\UrlGeneratorInterface;
final class CustomUrlGenerator implements UrlGeneratorInterface
{
public static function routes(RouteCollection &$routes): void
{
$routes->group('files', static function (RouteCollection $routes): void {
$routes->get('view/(:num)/(:segment)', [AssetConnectController::class, 'show/$1/$3'], [
'priority' => 100,
'as' => 'asset-connect.show',
]);
$routes->get('view/(:num)/variant/(:segment)/(:segment)', [AssetConnectController::class, 'show/$1/$2'], [
'priority' => 100,
'as' => 'asset-connect.show_variant',
]);
$routes->get('temporary/(:segment)/(:segment)', [AssetConnectController::class, 'temporary/$1'], [
'priority' => 100,
'as' => 'asset-connect.temporary',
]);
$routes->get('temporary/(:segment)/variant/(:segment)/(:segment)', [AssetConnectController::class, 'temporary/$1'], [
'priority' => 100,
'as' => 'asset-connect.temporary_variant',
]);
});
}
public static function params(Asset $asset, ?AssetVariant $variant, ?string $token = null): array
{
return [
'asset-connect.show' => [$asset->id, $asset->file_name],
'asset-connect.show_variant' => [$asset->id, $variant?->name, $variant?->file_name],
'asset-connect.temporary' => [$token, $asset->file_name],
'asset-connect.temporary_variant' => [$token, $variant?->name, $variant?->file_name],
];
}
}
This generator creates route-backed URLs like:
- Protected assets:
/files/view/{assetId}/{filename} - Protected variants:
/files/view/{assetId}/variant/{variantName}/{filename} - Temporary assets:
/files/temporary/{token}/{filename} - Temporary variants:
/files/temporary/{token}/variant/{variantName}/{filename}
Configuration
Set the generator in Config\Asset:
use App\UrlGenerators\CustomUrlGenerator;
use Maniaba\AssetConnect\Config\Asset as BaseAsset;
final class Asset extends BaseAsset
{
public ?string $defaultUrlGenerator = CustomUrlGenerator::class;
}
Notes
Public disks still return direct storage URLs from the disk configuration. To change public URLs, configure the disk's public_url or provide a Flysystem adapter with public URL support.
Protected disks and temporary URLs use AssetConnect routes, so authorization and HMAC temporary-token validation continue to run through AssetConnectController.