Laravel - Storage::url() does not return correct url

Asked

Viewed 436 times

0

I started in Windows a little while ago, at the moment the files are being stored inside the Storage folder, but I will use the cloud storage library of google cloud, can create and read files normally but the problem is when trying to get the file url to be able to preview the files in the application that will be consuming my Rest api.

This is the file structure:

storage
      |- app
      |    |- data
      |    |- public 
      |            |- images
      |                    |- produtos
      |                    |- supermercados
                                          |- 1
                                             | - 9d92b1d506a2065c4c0d132dbeb32217016d2a57f67a48ca7373cdff6bcd9ee6.jpeg
df974bf001ec886ab8ace6cdfab5850cf12ae66323927d55ccf591cc2f3079c0.jpeg
      |- framework
      |- logs 

I’m trying to get the link like this:

Storage::url('data/images/supermercados/' . $supermercado->id . '/' . $supermercado->foto);

Where id is the supermarket folder and photo is the name of the photo

But instead of getting a link I get this path:

 \/storage\/data\/images\/supermercados\/1\/df974bf001ec886ab8ace6cdfab5850cf12ae66323927d55ccf591cc2f3079c0.jpeg

How do I get a URL like http://site.com/imagem.jpge ?

  • Just set a route with that name /imagem.jpge and on the route written in the controller put something like public function imagem() { return Storage::download('data/images/supermercados/' . $supermercado->id . '/' . $supermercado->foto, 'image.jpg', [ 'Content-Type' => 'image/jpeg' ]); }

  • I had a similar problem yesterday. Take a test. Open the php artisan tinker and type env('APP_URL'). If you have returned other than what is in the file .env, it is very likely that your folder bootstrap/cache has saved some information empty or different from the current configuration. If I am not mistaken, it can be solved easily with php artisan cache:clear.

  • @Guilhermenascimento I am in doubt, I use a library to load the images called Glide, when passing the url it will perform a download or the library will load it from the server?

  • You said you were using a cloud, so this would be internal to the very API of the Laravel to solve, now if you did something of your own we have no way of knowing and the doubt is ambiguous. If you can detail it will help us to help you.

1 answer

1

I had a similar problem yesterday.

I was having the full url on my local machine and two more people running correctly, showing the full way, when calling Storage::url. However, when it was in production, the path was returned only with a / before.

What happens is that Laravel usually uses what is on APP_URL to mount the URL, in case of disk configuration public, in the archive config/filesystems.php. And the other Storage settings also have their own settings.

What happened was that Laravel created a cache inside the folder bootstrap/cache, with values that were quite different from the .env or inside the folder config.

The problem can be solved by removing or renewing the cache configuration, to force the update of these values.

You can run the command php artisan config:clear or php artisan config:cache to try to solve this.

A good test is to do the following:

1 - Open the .envand changes the value of APP_URL for any value, just to test. 2 - Open the php artisan tinker and type env('APP_URL'). Then give ENTER to display the value. 2 - Compare the value that is in .env and the value returned in your terminal.

If the values are different, try checking the folder bootstrap/cache has the file config.php (which is the configuration cache file).

If you have permission issues, you cannot remove with the command php artisan config:clear. You can choose to delete the file config.php manually.

NOTE: The configuration cache problem caused me problems both in URL mounting on disk public how much in the cloud what use. Both cases were displaying information different than what was configured or expected.

  • I did what you said APP_URL is returning localhost, but in Storage::url() returns nothing

  • In case, you checked the file filesystems.php? Generally, where is configured as disks.public, has an excerpt that contains the url assembly, thus 'url' => env('APP_URL') . '/storage.

  • Another thing: Flame echo Storage::url('') on any route and calls Storage::url('') in the php artisan tinker. See if the results are identical. I was having problems when calling via Browser and Via Terminal. The results were different.

  • @Samuelives run the command ls bootstrap/config and see if anything pops up. Of course, if you’re using a Unix-like system. In Windão, just go into the folder and see if there’s anything there

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

  • If root points to public then why Storage::put() puts date?

  • Because root is the root. The path you pass as an argument of put, is added what is defined in root. Example: Storage::put('pasta/arquivo.txt', 'meu arquivo') will create the way storage/app/public/pasta/arquivo.txt.

  • Storage::put('data/Juno/token.json', json_encode($tokenData)); it should be going to public

  • Laravel separates these files into the folder public, because when it’s invoked php artisan storage:link, a symbolic link (shortcut) is created inside the public folder, to make available the view of the files through the $url/storage/$caminho_do_arquivo.

  • About the folder you are storing, it is always best to check which Adapter is configured in filesystems.default. You can look at the file config/filesystems.php or even call on php artisan tinker: config('filesystems.default')

  • All configured as local, php Artisan Storage:link creates the public folder but only with a . gitignore

  • I managed to solve the problem however, whenever I send a file I will have to open the terminal and type: php Artisan Storage:link? And why Laravel duplicates files by creating a Storage folder in the public directory?

  • It doesn’t duplicate. As I said before, this is a symbolic link (it’s like a windows shortcut)

  • 1

    Yes, it is that in vs code it does not show as shortcut, but when checking in the file explorer I saw that it is a same link, problem solved, just access with Asset()

Show 9 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.