yiisoft/aliases
yiisoft/aliases stores and resolves path aliases (strings starting with @) for filesystem paths or URLs. Define aliases like @root, @vendor, @bin and expand them on demand, supporting nested aliases (e.g., @bin => @vendor/bin) without checking path existence.
yiisoft/aliases package provides a structured way to manage named paths/URLs (e.g., /app/config, /public/assets), which aligns with Laravel’s need for configurable, maintainable paths (e.g., storage paths, asset paths, or custom route aliases). It is particularly useful in:
config/aliases.php) to avoid hardcoding.config() helper and config/paths.php (if used).Storage facade or custom path resolvers.register() and boot() methods in a custom provider.config/aliases.php and merged with Laravel’s existing config.config() helper or create a custom facade (e.g., Alias::get('storage')).Storage::disk() already handles path resolution for filesystems. This package is more general-purpose (e.g., for URLs, API endpoints, or arbitrary paths).Route::alias() or replace custom route macro solutions.Cache::remember()).alias1 depends on alias2 which depends on alias1).config/aliases.php, environment variables, database?){env}://{path})?bind('path.storage', fn() => Alias::get('storage')))?null? Use default?)Alias::get() in PHPUnit?)storage_path())?config/aliases.php and publish them via a package service provider.Alias) or extend Laravel’s config() helper..env to override aliases (e.g., ALIAS_STORAGE=/custom/path).dump or generate aliases (e.g., php artisan alias:list).composer.json autoload paths.base_path('storage'), hardcoded URLs in routes)./api/v1, /uploads).base_path('app/config') with Alias::get('config').storage_path() → Alias::get('storage')).route('home') → Alias::get('route.home')).deprecated() helper for legacy path usages.composer.json).AliasManager to support Laravel-specific features (e.g., caching, environment variables).// app/Providers/AliasServiceProvider.php
use Yiisoft\Aliases\AliasManager;
public function register()
{
$this->app->singleton('aliases', fn() => new AliasManager([
'storage' => storage_path('app'),
'public' => public_path(),
]));
}
composer require yiisoft/aliases.php artisan vendor:publish --tag=aliases-config.config/aliases.php:
return [
'storage' => env('ALIAS_STORAGE', storage_path('app')),
'api.url' => env('API_URL', 'https://api.example.com'),
];
$this->app->bind('aliases', fn() => new AliasManager(config('aliases')));
app/Helpers/AliasHelper.php):
if (!function_exists('alias')) {
function alias(string $name): string {
return app('aliases')->get($name);
}
}
// Before
$path = base_path('storage/app');
// After
$path = alias('storage');
Route::get(alias('api.url').'/users', [UserController::class, 'index']);
$this->app->instance('aliases', new AliasManager(['storage' => '/fake/path']));
config/aliases.php), reducing duplication.aliases.php.Alias::get() calls may add overhead (mitigate with Laravel’s cache).ALIAS_DATABASE=/var/lib/mysql).How can I help you explore Laravel packages today?