I want to render/show an image in a view on backend, but the image location is in frontend/web/uploads.
I tried doing this on the DetailView::widget():
...
[
'attribute'=>'image',
'value'=>('/frontend/web/uploads/'.$model->image),
'format' => ['image',['width'=>'100','height'=>'100']],
],
But the image is not showing up. Any help?
Yes it is possible to access frontend/web/uploads to backend view.
First add urlManagerFrontend in backend->config->main.php like
'components' => [
'urlManagerFrontend' => [
'class' => 'yii\web\urlManager',
'baseUrl' => 'frontend/web/', //Access frontend web url
],
],
And then access the frontend/web/ to the backend like
Yii::$app->urlManagerFrontend->baseUrl.'/uploads/your_image.jpg'
In DetailView::widget like
[
'attribute'=>'image',
'value'=> function ($model) {
return Html::img(Yii::$app->urlManagerFrontend->baseUrl.'/uploads/'.$model->image, ['alt' => 'No Image', 'width' => '10px', 'height' => '10px']);
},
'format' => 'raw',
],
If this is the only place where you're linking to frontend from backend, the easiest way would be defining alias in your backend config:
Yii::setAlias('#frontendUploads', 'https://repasca.devs/uploads');
And use this alias for building file URL:
[
'attribute' => 'image',
'value' => Yii::getAlias('#frontendUploads') . '/' . $model->image,
'format' => ['image', ['width' => '100', 'height' => '100']],
],
Related
I need to load data with ajax to a Kartik Select2 but only when certain input changes values. Kind of like...
<?= $form->field($model, 'id_list')->widget(Select2::classname(), [
'data' => [],
'theme' => Select2::THEME_BOOTSTRAP,
'options' => [
'placeholder' => 'List',
],
'pluginOptions' => [
'allowClear' => true,
'action' => '#input_first'.change // or something
'ajax' => [
'url' => Url::to(['/list']),
'dataType' => 'json',
'data' => new JsExpression('function(params) { return {q:params.term}; }')
],
],
]);?>
Have you tried to use Select2 with DepDrop plugin (http://demos.krajee.com/widget-details/depdrop)?
I have a base64 image, and I want to display it in the following dataView
<?= DetailView::widget([
'model' => $model,
'attributes' => [
'nome:ntext',
'img1:ntext', // here is the base64 string that is also accessed by "$ model-> img1"
],
]) ?>
I tried the following code, but it did not work
[
'attribute' => 'img1',
'value' => base64_decode($model->img1),
'format' => ['image', ['width' => '100', 'height' => '100']]
],
Try something like this
[
'attribute' => 'img1',
'value' => 'data:image/png;base64,' . $model->img1,
'format' => ['image', ['width' => '100', 'height' => '100']]
],
or
[
'attribute' => 'img1',
'value' => 'data:image/jpeg;base64,' . $model->img1,
'format' => ['image', ['width' => '100', 'height' => '100']]
],
depending on image type.
BTW: If you're storing images in database you may want to read Store pictures as files or in the database for a web app? (and tens of duplicates). It is quite likely that you will have a lot of problems because of this approach.
How can I make a URL in DetailView row on yii2?
For example: blow code is something like custom row in GridView, but as you know DetailView is different and this code not works in DetailView.
<?= DetailView::widget([
'model' => $model,
'attributes' => [
[
'attribute' => 'file',
'label' => 'File',
'fomat' => 'Url', // I want something like this
'value' => Html::a('Dowaload The File', ['files/uloads']),
],
])
I have not found my problem's solution in similar questions.
Please help me to create Url row in DetailView.
You can use either the shorthand url formatter (that does not provide much customization options):
'attributes' => [
'file:url'
]
Or the raw format with which you can customize everything:
'attributes' =>[
[
'attribute'=>'file',
'label'=>'File',
'format'=>'raw',
'value'=>Html::a('Download the File', url),
],
...
You can create a download link to the file inside the detail view in the following way, I am assuming that the file path for download is coming from the database field file.
<?= DetailView::widget([
'model' => $model,
'attributes' => [
[
'attribute' => 'file',
'label' => 'File',
'value' => function ($model) {
return Html::a('Download The File', $model->file);
},
'format' => 'raw',
],
]
]);
I want to declare a method actionViewSlug($slug) in ScholarshipController in Yii2 REST api, My method is showing Not Found that too not in REST manner i.e. JSON.
Here is my Url Config for ScholarshipController
[
'class' => 'yii\rest\UrlRule',
'controller' => ['scholarship'],
'extraPatterns' => [
'POST filters' => 'filters',
'GET {slug}' => 'view-slug',
],
'tokens' => [
'{slug}' => '<slug>'
],
],
This is behaviors() function in ScholarshipController
public function behaviors()
{
return [
[
'class' => 'yii\filters\ContentNegotiator',
'only' => ['view', 'index', 'filters', 'view-slug'], // in a controller
// if in a module, use the following IDs for user actions
// 'only' => ['user/view', 'user/index']
'formats' => [
'application/json' => Response::FORMAT_JSON,
],
],
'corsFilter' => [
'class' => \yii\filters\Cors::className(),
'cors' => [
'Origin' => ['*'],
'Access-Control-Request-Method' => ['GET', 'POST', 'PUT', 'PATCH', 'DELETE', 'HEAD', 'OPTIONS'],
'Access-Control-Request-Headers' => ['*'],
'Access-Control-Allow-Credentials' => null,
'Access-Control-Max-Age' => 86400,
'Access-Control-Expose-Headers' => [],
],
],
];
}
Try this URL rule may it should help you.
But befor proceeding with this rule I want to clear you if you are using a module for rest API you must need to define your module ID too in rule either using prefix or direct
[
'class' => 'yii\rest\UrlRule',
'controller' => '<moduleID>/scholarships',
'tokens' => [
'{slug}' => '<slug>',
],
'extraPatterns' => [
'GET,HEAD {slug}' => 'view-slug',
]
],
and where is youe actionViewSlug($slug);
It is good if you share your controller code too. It's simple to use slug with different actions. One more thing here I would like to know why you are defining viewSlug action? Why you are not using the existing view action with slug?
Thanks
I'm using a module yii2-images by CostaRico for my application and it works fine. This module can rename, crop and store images in a specific folders and it does job well. But when I try to render an image in my view all I can get is a path like this:
/yii2images/images/image-by-item-and-alias?item=Product1&dirtyAlias=b6e73413ff-1_50x50.jpg
This is the render widget code:
<?php $imageOne = $model->getImage(); ?>
<?= DetailView::widget([
'model' => $model,
'attributes' => [
'id',
'title',
'alias',
'price',
[
'attribute' => 'imageOne',
'value' => '<img src="' . $imageOne->getUrl('50x50') . '">',
'format' => 'raw',
],
'status',
'frontpage',
'primary_category',
'sku',
'short_desc:ntext',
'full_desc:html',
],
]) ?>
Normally, I would wrap it with img tag and the image should be rendered. However, it won't happen. Browser shows Status 200, OK. Image does exists and if I will use a path like below I can see it in my view.
/upload/cache/Products/Product1/b6e73413ff-1_50x50.jpg
Also, I've tried to render this image using DetailView widget adding format options like raw and html but no luck.
I've seen some guys on YouTube using this modules and they had the same kind of path with parameters and everything is ok.
What could be the problem? Is it some routing issue? I haven't changed the original module.
Routs
'urlManager' => [
'enablePrettyUrl' => true,
'showScriptName' => false,
'enableStrictParsing' => false,
'rules' => [
'category/<alias>/page-<page:\d+>' => 'category/view',
'category/<alias>' => 'category/view',
'product/<id:\d+>' => 'product/view',
'page/<alias>' => 'page/view',
'search' => 'category/search',
],
],
if you have RBAC or limited access for guest
'as access' => [
...
'yii2images/*',
],
and route
'image' => 'yii2images/images/image-by-item-and-alias',