php - 如何使用 Yii2 DetailView 在后端渲染前端图像?
问题描述
我想在后端的视图中渲染/显示图像,但图像位置在frontend/web/uploads
.
我试过这样做DetailView::widget()
:
...
[
'attribute'=>'image',
'value'=>('/frontend/web/uploads/'.$model->image),
'format' => ['image',['width'=>'100','height'=>'100']],
],
但是图像没有显示出来。有什么帮助吗?
解决方案
是的,可以访问frontend/web/uploads
查看backend
。
先加urlManagerFrontend
个backend->config->main.php
like
'components' => [
'urlManagerFrontend' => [
'class' => 'yii\web\urlManager',
'baseUrl' => 'frontend/web/', //Access frontend web url
],
],
然后访问frontend/web/
到backend
like
Yii::$app->urlManagerFrontend->baseUrl.'/uploads/your_image.jpg'
在 DetailView::widget 之类的
[
'attribute'=>'image',
'value'=> function ($model) {
return Html::img(Yii::$app->urlManagerFrontend->baseUrl.'/uploads/'.$model->image, ['alt' => 'No Image', 'width' => '10px', 'height' => '10px']);
},
'format' => 'raw',
],
推荐阅读
- ios - 过渡动画中的闪烁(模拟器?)
- javascript - javascript - 在承诺的 then 部分访问它
- go - golang http服务器在收到信号后优雅关闭
- symfony - 为什么我似乎不能将变量从控制器传递到 symfony 4 中的表单?
- ms-access - 如果部分字段与其他记录不同,则限制输入
- asp.net - 将 WebForm 路由到 MVC
- javascript - 是否有停止 JWPlayer (8.7.3) 广告和/或广告连播的功能?
- android - 在同一线程上调用新协程时如何取消旧协程
- django-templates - 将基于类的 ListView 作为模板片段包含在 TemplateView 中
- excel - 使用 Index( match( max( countifs 并在第一次回答后返回错误值