1

I have following image tag in blade.

{!! Form::image('/MySplash/1/file.png', $alt="Photo", null) !!}

Below is my code in Route file..

Route::get('MySplash/{IconID}/{UniqueFileName}', function ($IconID, $filename)
{
    $path = storage_path() . '/SplashScreenIcons/' . $IconID . '/' . $filename;

    if(!File::exists($path)) {
        abort(404);
    }

    $file = File::get($path);
    $type = File::mimeType($path);

    $response = Response::make($file, 200);
    $response->header("Content-Type", $type);

    return $response;;
});

Finally I get the image url like below.

http://localhost/SportsApp/public/MySplash/1/file.png

What's the problem

  1. It does not go into route.
  2. Url is showing public. whereas the image folder is in storage directory.

Am I missing anything?

Pankaj
  • 9,749
  • 32
  • 139
  • 283

1 Answers1

1

Form::image() creates a HTML input that uses a file resource. Rendering this input and the path to its image will not directly call your GET route. The framework assumes you provided a static path and will simply map it to the public directory by default, for a browser to retrieve the resource.

You keep the icons in the storage, which means the files are not publicly available. If you're working in a Linux-based environment or you use Homestead locally, the simplest thing to do to make the icons available on client side is to create a symlink from your public folder to storage, like this:

ln -s /home/vagrant/Code/myproject/storage/SplashScreenIcons /home/vagrant/Code/myproject/public/MySplash

Then, you can use the Form helper as usual:

{!! Form::image('/MySplash/1/file.png', $alt="Photo", null) !!}

or use the asset() helper:

<input src="{{'MySplash/1/file.png'}}" type="image">

One thing to keep in mind is that you'll need the symlink on your production as well, e.g. as a deployment hook.

lesssugar
  • 15,486
  • 18
  • 65
  • 115
  • At least you know why the routing doesn't work, I guess. Pretty sure there's a way to create a symlink on Windows, though. And in the end, how likely is it your production server will run on Windows? Also, you should really try Laravel Homestead out, it's easy to set-up and very convenient. Creating a route to handle simple file resource retrieving (provided out of the box) seems like an overkill, something I would re-think. Good luck. – lesssugar Jun 05 '16 at 13:41
  • I tried this also: http://stackoverflow.com/questions/30191330/laravel-5-how-to-access-image-uploaded-in-storage-within-view but not useful. Can you please check the solution there? – Pankaj Jun 05 '16 at 15:26