0

I’m quite new to Laravel, so patience with me.

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.

Goal 1:

I want to load images using the blade {{ asset() }} helper in a template page. The thing is that I want to load the images from a subfolder that resides inside the resources folder:

/resources/app_files_layout

I know that I could do this by moving the folder to the public directory, but that’s not what I have in mind. My intention is also to advance on a deeper learning and understanding on the laravel framework and try to maintain an architectural fidelity with my framework on other languages.

Goal 2:

After I manage to load the images from the /resources/app_files_layout through {{ asset() }} helper in blade, I’d like to use an alias for the route in the source code. Example: Instead of displaying in the output HTML in the browser http://localhost/app_files_layout/image-name.jpg, I’d like it to display: http://localhost/images/image-name.jpg and never show the user that the files are in a app_files_layout folder.

Is what I’m trying to do possible at all in laravel?

Worst case scenario:

If laravel doesn’t have the necessary architecture to do what I want, I could settle for setting up a route that displays the image in the /resources/app_files_layout folder. That is: when I access on the browser http://localhost/images/image-name.jpg should load an image located in /resources/app_files_layout/image-name.jpg.

I tried to follow this stackoverflow suggestion: https://stackoverflow.com/a/38736973/2510785 However, returned me an error like so:

The requested resource /files-layout-test/backend-layout-header-tb02-03.jpg was not found on this server.

Thanks, in advance!

Jorge Mauricio
  • 411
  • 6
  • 18
  • If you want to dig in, here are the hints for you 1) https://stackoverflow.com/questions/32810231/add-public-to-asset-path-in-laravel#answer-32839086 . Create new class and extend proposed one. Create new method called `assetImage` and specify your path. But try not to hardcode it inside of the method. Create some kind of `.env` variable for this and assign it also to config with default value. 2) How about `aliases` https://stackoverflow.com/questions/21399789/nginx-how-to-create-an-alias-url-route ? 3) Framework is not a thing inside of itself. Use tools which are at your disposal. – Sergey Ligus May 21 '22 at 19:20
  • `/public` is the only path "publicly" available (via URL). Either create a link to your specific folder, create a webpack entry that copies your files to a folder inside the `/public` folder or link that folder (like it is done with the `/storage` folder when running `php artisan storage:link`. – brombeer May 21 '22 at 19:37
  • Hi @SergeyLigus, I like the first approach. I´m going to try it, but as I said, I´m still a bit new to Laravel, so I may ask dumb questions. I found the file with the asset method in ```syncsystem-laravel8-v1\vendor\laravel\framework\src\Illuminate\Routing\UrlGenerator.php```. So, my first question: do I create this new method in this same file? My concern is that if I do a ```composer install```, it would be erased. If not in this file, where would I created and how. Can you give a basic example? – Jorge Mauricio May 22 '22 at 17:10
  • @SergeyLigus, I also took a look at the second approach, but the intention would be to create a route alias in web.php file that could serve my asset files in resources subfolders. – Jorge Mauricio May 22 '22 at 17:13

1 Answers1

0

I managed to do what I wanted for this one. All credit for @donkarnash on this question that I posted for the worst case scenario for creating a route alias for it: Laravel 8 – create route alias for image folder inside public directory

I used the symbolic link approach, almost similar to what @sergey-ligus had mentioned in the comments.

1 – edit laravel \config\filesystems.php file: Originally, looks like this:

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

After I edited, looked like this:

'links' => [
    public_path('storage') => storage_path('app/public'),
    public_path('files-layout') => resource_path('app_files_layout'),
], 

2 - Then, in the command terminal, I executed the following command:

php artisan storage:link

3 - Finally, I ran the server and tested out. Not only could I access the file in the browser via: http://localhost:8000/files-layout/backend-layout-header-tb02-03.jpg, but also could still use laravel asset blade helper.

Blade file:

{{ asset('/files-layout/backend-layout-header-tb02-03.jpg') }}
Jorge Mauricio
  • 411
  • 6
  • 18