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',
Related
I don't know what's wrong with my code because i think, i have following all the step from github krivochenko/yii2-cropper But button to cropping or delete upload doesn't work and and also the css displayed on the form is not neat like this :
i'm not see any error in console, and i want to include the code i made :
this is in view :
use budyaga\cropper\Widget;
<div class="form-group col-sm-8 col-md-8 col-lg-6">
<?= $form->field($model, 'imageFile')->widget(Widget::className(), [
'uploadUrl' => Url::toRoute($uploadPhoto['url']),
]) ?>
</div>
then this is in controller:
return $this->render('input', [
'model' => $model,
'uploadPhoto' => [
'class' => 'budyaga\cropper\actions\UploadAction',
'url' => 'content/user/create',
'path' => $this->baseApp.'/files/uploads',
]
]);
So what's wrong with this code?
finally, i can understand what wrong with my code.
I need to adjust css from this widget in my own with add param in widget input form.:
<?= $form->field($model, 'imageFile')->widget(Widget::className(), [
'uploadUrl' => Url::toRoute($uploadPhoto['url']),
'cropAreaHeight' => 'auto' //here
]) ?>
i put Upload Photo index into create action, Upload Photo should be placed in function action(general) like this:
public function actions()
{
return [
'error' => [
'class' => 'yii\web\ErrorAction',
],
'uploadPhoto' => [
'class' => 'budyaga\cropper\actions\UploadAction',
'url' => 'dir_to_save_the_result_of_crop',
'path' => 'path_to_save_the_result_of_crop',
]
];
}
And this widget krivochenko/yii2-cropper can works also really easy to use in yii2
I have setup my Yii2 powered website but I will like to get some of my static pages in sub-directories and then display the name of the sub-directory in the url. For example, have in site/hotel/pacific.php and site/mini-rooms/standard.php and then access them using xyz.com/hotel/pacific and xyz.com/mini-rooms/standard.
I have tried to follow some examples online but I am not sure why the are not working. Some of the examples are also for yii and not yii2.
Here is what I did:
In the SiteController.php, I had created a static page before using the code below and I can visit it atxyz.com/hotel/pacific. But now I am moving it into a subdirectory hotel. It was formerly inside the views/site folder:
public function actionPacific()
{
return $this->render('pacific');
}
I then created a folder name pages inside the view folder. Inside the pages folder, I created hotel folder and inside hotel folder I placed pacific.php file so I have views/site/pages/hotel/pacific.php
Also in the SiteController.php, I added this:
public function actions()
{
return [
'error' => [
'class' => 'yii\web\ErrorAction',
],
'captcha' => [
'class' => 'yii\captcha\CaptchaAction',
'fixedVerifyCode' => YII_ENV_TEST ? 'testme' : null,
],
'page' => [
'class'=>'CViewAction',
],
'hotels' => [
'class'=>'CViewAction',
'basePath' => 'pages/hotels'
],
];
}
I then configure my config/web.php by adding the code below:
'urlManager' => [
'class' => 'yii\web\UrlManager',
'enablePrettyUrl' => true,
'showScriptName' => false,
'enableStrictParsing'=>false,
'rules' => [
'<alias:[\w\-]+>' => 'site/<alias>',
'<controller:\w+>/<id:\d+>'=>'<controller>/view',
'<controller:\w+>/<action:\w+>/<id:\d+>'=>'<controller>/<action>',
'<controller:\w+>/<action:\w+>'=>'<controller>/<action>',
],
],
I got 404 error when I visit xyz.com/hotel/pacific.
How can I properly do this for both examples - xyz.com/hotel/pacific and xyz.com/mini-rooms/standard.
What I understood that you are mixing Yii 1 & 2 as CWebView is used in Yii1 and not Yii2. So what you need is probably yii\web\ViewAction, which will represent a single action. There might be better answers someone else could suggest.
So if you have a directory.
views/site/hotel/pacific.php
views/site/hotel/five-star.php
And you want to access the pacific.php view by typing in the URL xyz.com/hotel/pacific and five-star.php with xyz.com/hotel/five-star.
Then you should configure them like below
public function actions()
{
return [
'error' => [
'class' => 'yii\web\ErrorAction'
],
'pacific' => [
'class' => 'yii\web\ViewAction',
'defaultView' => 'pacific',
'viewPrefix' => 'hotel'
],
'five-star' => [
'class' => 'yii\web\ViewAction',
'defaultView' => 'five-star',
'viewPrefix' => 'hotel'
],
'captcha' => [
'class' => 'yii\captcha\CaptchaAction',
'fixedVerifyCode' => YII_ENV_TEST ? 'testme' : null
]
];
}
and then update the urlManager like below
'urlManager' => [
'class' => 'yii\web\UrlManager',
'enablePrettyUrl' => true,
'showScriptName' => false,
'enableStrictParsing'=>false,
'rules' => [
'/' => 'site/index',
'<alias:[\w\-]+>' => 'site/<alias>',
'hotel/<alias:[\w\-]+>' => 'site/<alias>',
'<controller:\w+>/<id:\d+>' => '<controller>/view',
'<controller:\w+>/<action:\w+>/<id:\d+>' => '<controller>/<action>',
'<controller:\w+>/<action:\w+>' => '<controller>/<action>'
]
],
Now if you type in the browser xyz.com/hotel/pacific or xyz.com/hotel/five-star it will show you the corresponding views.
Hope it solves your problem.
I decided to create HotelController.php inside the controller folder and then create hotel folder under views folder. I then added pacific.php inside the hotel folder and add the action code inside HotelController.php as below.
public function actionPacific()
{
return $this->render('pacific');
}
It works as I wanted although I am not sure if there are better alternatives. I removed the rules in the url manager as I did not need them now. I only had to just go to mysite.com/hotel/pacific to display the page. I can create as many page as I need inside the folder and just add their action code.
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']],
],
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'm using the FriendsOfCake CakePDF Plugin with wkhtmltopdf to render my views as PDF.
I also use their Search plugin to filter view data with a form.
For now, when I print the data in the view it always renders all view data into the PDF and not only the filtered data that is displayed on screen.
Is there any way to do this? I can't find anything that mentions such a case in the Plugin Docs. It seems like the PDF plugin always reloads the page in its default state or rather loads the default query from the index function instead of the filtered data. Since this is my first CakePDF project I don't really get what I have to do to make it render the filtered data instead. Can anybody help with that?
Here is what my main files look like so far:
class PaintingsController extends AppController
{
public function index()
{
$query = $this->Paintings
->find('search',
$this->Paintings->filterParams($this->request->query))
->contain(['Artists',
'Tickets' => function ($q) {
return $q->where(['Tickets.active' => false]);
}
]);
$this->viewBuilder()->options([
'pdfConfig' => [
'orientation' => 'portrait',
'filename' => 'paintings.pdf'
]
]);
$this->set('paintings', $this->paginate($query));
$this->set('_serialize', ['paintings']);
}
}
class PaintingsTable extends Table
{
public function searchConfiguration()
{
$search = new Manager($this);
$search->like('title', [
'before' => true,
'after' => true,
'field' => $this->aliasField('title'),
'filterEmpty' => true
])->value('property', [
'field' => $this->aliasField('property'),
'filterEmpty' => true
])->like('artist_name', [
'before' => false,
'after' => true,
'field' => $this->Artists->target()->aliasField('surname'),
'filterEmpty' => true
])->value('technique', [
'field' => $this->aliasField('technique'),
'filterEmpty' => true
]);
return $search;
}
}
In Template\Paintings\index.ctp
... data in tables ...
<?= $this->Html->link('Save as PDF',[
'action' => 'index',
'_ext' => 'pdf'],[
'class' => 'create-pdf-link',
'target' => 'blank'
]) ?>
Then everything gets rendered in Templates\Paintings\pdf\index.ctp without the applied filtering.
Your PDF link won't contain any filter paramters, so there is no reload or anything, it just won't do any filtering.
The current query is not being incorportated automatically when generating links/URLs, you have to explicitly pass it to the URL array on your own, like
$this->Html->link(
'Save as PDF',
[
'action' => 'index',
'_ext' => 'pdf'
] + $this->request->query, // there it goes
[
'class' => 'create-pdf-link',
'target' => 'blank'
]
);
See also Cookbook > Routing > Generating URLs
FIY, in CakePHP 2, things are a bit different, you would use
$this->request->params['named']
instead of
$this->request->query
like
$this->Html->link(
'Save as PDF',
[
'action' => 'index',
'_ext' => 'pdf'
] + $this->request->params['named'],
[
'class' => 'create-pdf-link',
'target' => 'blank'
]
);