0

I’m in the middle of the process of replicating a framework that I developed in node / react to laravel. Right now, I’m adjusting the main architecture and currently working on a blade master page.

My original idea (Laravel 8 – use blade asset to display image, but loading from resources subfolder) didn’t work, so I’m trying a new approach to set up how I want my asset files to be served.

The assets in question is basically images for layout purposes. I organized the directory like so:

public/app_files_layout

Inside it, I have a bunch of image files that I want to access. The thing is that I don’t want to access like http://localhost:8000/app_files_layout/image-name.jpg. My intention is to access like: http://localhost:8000/images/image-name.jpg, but I want to maintain the directory names I created intact, so it can have a high fidelity architectural organization similar to my framework that I built in other languages.

I figured that I would set up a simple routing logic for it in Laravel web.php file. I followed the suggestion from this stackoverflow question: https://stackoverflow.com/a/38736973/2510785

However, when I try to access via browser through the following address http://localhost:8000/files-layout-test/image-name.jpg, returned me an error like so:

The requested resource /files-layout-test/image-name.jpg was not found on this server.

I stripped the code just to try to find out what could be wrong, and this is what I did to debug it:

Route::get('/files-layout-test/{filename}', function($filename){
    echo 'debug';
});

The strange behavior is that, when I try to access without the file extension (ex: http://localhost:8000/files-layout-test/image-name), it goes through, but I need the file extension to be there.

Any ideas on how I could get this done? Note: I’m new to Laravel, so the answer may be simple.

Jorge Mauricio
  • 411
  • 6
  • 18
  • Why not create `public/images` folder/directory and create a symlink to `public/app_files_layout`? That ways you can maintain the directory names and still get the desired output. – Donkarnash May 22 '22 at 19:56
  • @Donkarnash, is the symlink you´re talking about created through artisan command line or in server configuration file? I wouldn´t want to create in server configuration file, since I´m looking for it to be compatible in linux and windows servers. Also, an idea that I have is that I would be able to change the refence to the physical path and the route path through configuration variables. – Jorge Mauricio May 22 '22 at 20:15
  • You can have the values stored in a config file and create a custom Artisan command to execute the symlink based on the values defined in the configuration file. That way you can execute the symlink execution in a deployment script as well. – Donkarnash May 22 '22 at 20:22
  • @Donkarnash, interesting. Can you post an example? As I said, I´m a bit new to laravel, but I´m in the process of deep learning. It´s not exactly what I´m looking for in this post, but I can test it and if it works, I´ll vote you answer up. – Jorge Mauricio May 22 '22 at 20:27

1 Answers1

1

Basically for simple stuff like creating a symlink for public/images and public/app_files_layout you can use the built-in storage:link command.

In your config/filesystems.php file, you can define the symlinks you want to create

'links' => [
    public_path('storage') => storage_path('app/public'),
    public_path('images') => public_path('app_files_layout'),
],

Then you can run php artisan storage:link and it will create all the symlinks defined the links array in config/filesystems.php. No need to create any custom Artisan command. You can read more at Laravel Docs

With the above symlink created you can use the asset() helper to generate the urls for assets which are actually in public/app_files_layout using asset('images/filename.ext').

You can also access public/app_files_layout/image-name.ext at http://localhost:8000/images/image-name.ext once the symlink is created.

However if you want to add some other logic or say you want to get user input for creating symlinks then you can define your own custom Artisan command using the storage:link command as starting point

Donkarnash
  • 12,433
  • 5
  • 26
  • 37
  • You know what? It worked very well! And I think it´s actually the approach I´m going to take to for the architectural setup I´m building. I just won´t give this the right answer because I still want to know how I could do this with route, for future features. – Jorge Mauricio May 22 '22 at 21:11