Custom URL in Laravel Storage

Aleksander Tabor
2 min readNov 25, 2019

--

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.

Installation

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):

cd customstoreurl
php artisan storage:link

Currently, you will have a created symlink:

public/storage to storage/app/public

Storage symlink in Laravel

I will show you an example with an image file (laravel-logo.png) that I’ve just added to storage/app/public.

Laravel Storage symlink with files

All files from storage/app/public can be accessed with a link below:

http://domain.com/storage/laravel-logo.png

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:

cd public
mv storage custom
Renaming storage symlink in Laravel

After that operation your current URL to access files from Storage has changed:

http://domain.com/custom/laravel-logo.png

Setting up The Storage Facade

It’s not yet finished because if you use the Storage facade for retrieving files like:

Storage::url('laravel-logo.png');

You will still have the old link /storage/ within.

The solution:

  1. Add in your .env (remember to restart your server after changing environment variables):
FILESYSTEM_DRIVER=public

2. The last part is to edit one line in your public disk configuration:

config/filesystems.php

'public' => [  'driver' => 'local',  'root' => storage_path('app/public'),  'url' => env('APP_URL').'/storage',  'visibility' => 'public',],

to

'public' => [  'driver' => 'local',  'root' => storage_path('app/public'),  'url' => env('APP_URL').'/custom',  'visibility' => 'public',],

That’s all.

What’s next?

For more information about Laravel Storage (storing, updating and deleting files etc.) you can look for the official Laravel documentation.

--

--

Recommended from Medium

Lists

See more recommendations