Php Yii2:重写URL后无法在后端和前端显示图像

Php Yii2:重写URL后无法在后端和前端显示图像,php,yii2,yii2-advanced-app,Php,Yii2,Yii2 Advanced App,我已更改了前端/web和后端/web url,正在应用程序根目录下的上载文件夹中上载文件,例如“/yii2app/uploads”,但无法显示图像 使用htaccess更改前端和后端url。所有链接都工作正常。此外,我正在根文件夹中上载文件。从后端应用程序,我上传到根文件夹的图像,这是罚款。但我无法查看图像 我的根文件夹访问权限 Options -Indexes IndexIgnore */* #follow symbolic links Options FollowSymlinks Rew

我已更改了前端/web和后端/web url,正在应用程序根目录下的上载文件夹中上载文件,例如“/yii2app/uploads”,但无法显示图像

使用htaccess更改前端和后端url。所有链接都工作正常。此外,我正在根文件夹中上载文件。从后端应用程序,我上传到根文件夹的图像,这是罚款。但我无法查看图像

我的根文件夹访问权限

Options -Indexes

IndexIgnore */*

#follow symbolic links
Options FollowSymlinks
RewriteEngine on
#without trailing slash
RewriteRule ^admin(/.+)?$ backend/web/$1 [L,PT]
#with trailing slash
RewriteRule ^admin/(.+)?$ backend/web/$1 [L,PT]
RewriteRule ^(.+)?$ frontend/web/$1
前端访问

RewriteEngine on
RewriteBase /yii2app

# if a directory or a file exists, use it directly
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d

# otherwise forward it to index.php/controller/action
RewriteRule (?!web\/) index.php/$1
RewriteEngine on
RewriteBase /yii2app/admin

# if a directory or a file exists, use it directly
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d

# otherwise forward it to index.php
RewriteRule . index.php
后端访问

RewriteEngine on
RewriteBase /yii2app

# if a directory or a file exists, use it directly
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d

# otherwise forward it to index.php/controller/action
RewriteRule (?!web\/) index.php/$1
RewriteEngine on
RewriteBase /yii2app/admin

# if a directory or a file exists, use it directly
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d

# otherwise forward it to index.php
RewriteRule . index.php
在config/main.php中,添加了home和base url

/yii2app - frontend
/yii2app/admin - backend
另外,我在common/config/bootstrap.php中创建了别名'@root'

Yii::setAlias('@root', realpath(dirname(__FILE__).'/../../'));
问题是当我尝试加载img时,它并没有显示出来。我尝试在根文件夹中上载图像。 请帮忙

更新

下面的代码是我用来查看gridview内部的图像(从后端),但不起作用

[
                'label'=>'Event Image',
                'attribute'=>'image_url',
                'format'=>'html',
                'value'=>function($model){
                    return Html::img(Yii::getAlias('@root')."/uploads/events/".$model->id."/".$model->image_url);
                }
            ],
此外,尝试从后端应用程序

[
                'label'=>'Event Image',
                'attribute'=>'image_url',
                'format'=>'html',
                'value'=>function($model){
                    return Html::img("../../uploads/events/".$model->id."/".$model->image_url, ['width' => '170px']);
                }
            ],

我搞错了吗?

您没有为根目录设置别名,但应该使用web目录中的符号链接并查看图像

前端
后端
web
目录中创建一个符号链接

前端 用于后端 然后像下面一样在前端或后端使用它们

<img src="/uploads/filename.jpg" />

注意:
/D
用于创建指向目录的符号链接

如何查看图像?在提供图像路径的位置添加代码,并且您正在根目录上上载,您提到的解决方案是使用
frontend/web
sa公共文件夹而不是根目录文件夹上载。请检查更新。我尝试在IMG标记的src中调用alias@root,但没有成功。我不确定,但由于我创建了一个共同的别名,我认为它会起作用。您还提到在common中上载图像,那么common文件夹是否只在前端和后端之间共享?我确实想过这样做,但我认为我犯了一些错误,因为我重写了url,不知何故无法访问图像。我有一个关于创建符号链接的建议。在这种情况下(对于yii2应用程序),在生产中创建符号链接以查看上传的图像是否明智?有没有其他方法可以在前端和后端上传图片和视图?更新:我尝试了这个解决方案,但没有成功。我创建链接并在视图中显示它,如下所示:
['label'=>'Event Image','attribute'=>'Image\u url','format'=>'html','value'=>函数($model){return html::img(“uploads/events/”$model->id.“/”$model->Image\u url,['width'=>'170px'])],
在开始时使用
/
,并且您不使用相对路径您只需使用
/path/to/file
前端或后端它与我在添加答案之前在本地系统上尝试的一样简单,只需删除任何额外的htaccess并仅保留默认值,我没有使用您的htaccess配置检查加载或上载的图像@MislamI最终发现了问题。查看url
http://localhost/yii2app/admin/events/uploads/events/2/First%20Event.png
,在这里应该是
http://localhost/yii2app/admin/uploads/events/2/First%20Event.png
,然后它就可以工作了,但我不知道为什么url中admin之后的事件,我可能配置错了。谢谢你的帮助。但是有一个问题,有没有最好的方法来删除带有漂亮url的前端/web和后端/web,我看过一些教程,他们将index.php从web文件夹中删除。它工作得很好。但其他链接不起作用,所以需要再次访问
mklink /D c:\xampp\htdocs\yii2app\frontend\web\uploads c:\xampp\htdocs\yii2app\uploads