laravel:如何显示 Public 文件夹中的资源

laravel: how to display the assets that are inside the Public folder

在服务器(共享主机)上,public 文件夹中的所有资产都不会显示。即

<img src="{{asset($painting->file)}}" >

输出这个

<img src="http://subdir.example.com/uploads/awJ4o8_miro1.jpg" >

它应该有一个 public 文件夹

http://subdir.example.com/public/uploads/awJ4o8_miro1.jpg

在我的 Public_html 文件夹中我有这个文件夹结构

PUBLIC_HTML
├───app
├───bootstrap
├───cgi-bin
├───config
├───database
├───public
│   ├───css
│   ├───img
│   ├───js
│   ├───uploads
│   └───videos
│   .htaccess(file)
│   .index.php(file)
├───resources
├───storage
└───tests
└   .htaccess(file)

两个 .htaccess 文件都像这个文件

DirectoryIndex public/index.php
<IfModule mod_rewrite.c>
    <IfModule mod_negotiation.c>
        Options -MultiViews
    </IfModule>

    RewriteEngine On

    # Redirect Trailing Slashes If Not A Folder...
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(.*)/$ / [L,R=301]

    # Handle Front Controller...
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^ public/index.php [L]
</IfModule>

权限

所以我的问题是如何设置我的配置 (.htaccess) 以便我可以在共享主机站点上显示我的资产。

您可以将此规则插入到最后一条规则的正上方,以将资产重定向到 public/:

DirectoryIndex public/index.php
<IfModule mod_rewrite.c>
    <IfModule mod_negotiation.c>
        Options -MultiViews
    </IfModule>

    RewriteEngine On

    # Redirect Trailing Slashes If Not A Folder...
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(.*)/$ / [L,R=301]

    RewriteRule ^uploads(/|$) /public%{REQUEST_URI} [L,NC,R=301,NE]

    # Handle Front Controller...
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^ public/index.php [L]
</IfModule>

您可以在资产 url 前加上 public 作为前缀:

{{ asset('public/' . $painting->file) }}