Custom URL in Laravel Storage
Laravel provides a great layer for working with local and cloud filesystems. If you work with files using the public disk and Laravel Storage facade, you will be struggling with customizing your URLs.
I will show you how to customize your Laravel Storage links.
First create a new Laravel project or use the existing one. I will do it on an completely new application.
laravel new customstorageurl
The next step is to create default symbolic link with the commands below (if you haven’t done it yet):
php artisan storage:link
Currently, you will have a created symlink:
public/storage to storage/app/public
I will show you an example with an image file (laravel-logo.png) that I’ve just added to storage/app/public.
All files from storage/app/public can be accessed with a link below:
Customizing Laravel Storage links
Our purpose is to change that /storage/ in URL. It can be customized with a little trick.
In order to do it, rename your symbolic link in /public from storage to custom:
mv storage custom
After that operation your current URL to access files from Storage has changed:
Setting up The Storage Facade
It’s not yet finished because if you use the Storage facade for retrieving files like:
You will still have the old link /storage/ within.
- Add in your .env (remember to restart your server after changing environment variables):
2. The last part is to edit one line in your public disk configuration:
'public' => [ 'driver' => 'local', 'root' => storage_path('app/public'), 'url' => env('APP_URL').'/storage', 'visibility' => 'public',],
'public' => [ 'driver' => 'local', 'root' => storage_path('app/public'), 'url' => env('APP_URL').'/custom', 'visibility' => 'public',],
For more information about Laravel Storage (storing, updating and deleting files etc.) you can look for the official Laravel documentation.