laravellaravel-valetvalet

Valet showing files under wrong Url


Currently running into issues on my server with links to files in my Laravel application.

I currently have a images folder in storage/app/public and I linked the storage/app/public folder to public with php artisan storage:link.

Now i have a image.jpg in the images folder. On my local system while using valet I can see the image with the tow following links:

  1. /images/image.jpg
  2. /storage/images/image.jpg

On my server only the second one works (which I assume is correct as there is no images folder directly in the public-directory.

Problem is, I often run into issues when deploying as customers can't see images online (as the link is not working). This wouldn't be an issue if I could not see images local while testing the UI.

Anyone also has this issue and can lead me to some kind of solution?


Solution

  • The issue is that the LaravelValetDriver has a fallback test; if the file doesn't exist in the public directory then it will check the public storage directory.

    You can find that here:

    if ($this->isActualFile($storagePath = $sitePath.'/storage/app/public'.$storageUri)) {
        return $storagePath;
    }
    

    You can prevent this behaviour by creating your own Valet Driver that extends the LaravelValetDriver and overrides the isStaticFile method, e.g:

    class NoStorageFallbackLaravelValetDriver extends LaravelValetDriver
    {
    
        public function isStaticFile($sitePath, $siteName, $uri)
        {
            if (file_exists($staticFilePath = $sitePath. '/public' . $uri)
           && is_file($staticFilePath)) {
                return $staticFilePath;
            }
    
            return false;
        }
    }