Home  >  Q&A  >  body text

Using Laravel 8's blade asset to display images but loading from resources subfolder

I'm pretty new to Laravel, so please bear with me.

I'm copying a framework I developed in Node/React into Laravel. Currently I'm tweaking the main architecture and am currently working on the blade master page.

Goal 1:

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

/resources/app_files_layout

I know I can do this by moving the folder to a public directory, but that's not what I want. I also aim to deepen my learning and understanding of the Laravel framework and try to maintain architectural fidelity to my framework on other languages.

Objective 2:

After I managed to load the image from /resources/app_files_layout via the {{ asset() }} helper in Blade, I wanted to use an alias of the path in the source code. example: I want it to display: http://localhost/images/image-name.jpg instead of http://localhost/app_files_layout/image-name.jpg in the output HTML in the browser and never display these to the user The files are located in the app_files_layout folder.

Is what I'm trying to do possible in Laravel?

Worst case scenario:

If Laravel doesn't have the necessary architecture to do what I want, I can set up a route that displays the image in the /resources/app_files_layout folder. That is: when I visit http://localhost/images/image-name.jpg on my browser, the image located in /resources/app_files_layout/image-name.jpg should be loaded.

I tried following this stackoverflow suggestion: https://stackoverflow.com/a/38736973/2510785 However, it returns me an error like this:

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

Thank you in advance!

P粉428986744P粉428986744368 days ago895

reply all(1)I'll reply

  • P粉928591383

    P粉9285913832023-11-10 09:47:01

    I successfully did this. All credit goes to @donkarnash Regarding this question I posted the worst case scenario for creating a routing alias for it: Laravel 8 - Creating a routing alias for an images folder in a public directory

    I used the symlink method, almost similar to the one mentioned by @sergey-ligus in the comments.

    1 – Edit laravel \config\filesystems.php file: It initially looks like this:

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

    After editing, it will look 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 it. Not only can I access the file in the browser at http://localhost:8000/files-layout/backend-layout-header-tb02-03.jpg, but I can still use the laravel assetblade helper.

    Blade file:

    {{ asset('/files-layout/backend-layout-header-tb02-03.jpg') }}

    reply
    0
  • Cancelreply