yii2mod / yii2-link-preview
LinkPreview widget renders page preview
Installs: 1 712
Dependents: 0
Suggesters: 0
Security: 0
Stars: 28
Watchers: 10
Forks: 12
Open Issues: 2
Type:yii2-extension
Requires
- embed/embed: ^2.6
- yii2mod/yii2-behaviors: *
- yiisoft/yii2: >=2.0.8
Requires (Dev)
This package is not auto-updated.
Last update: 2024-09-28 18:37:03 UTC
README
LinkPreview widget automatically retrieves some information from the content of the link.
Installation
The preferred way to install this extension is through composer.
Either run
php composer.phar require --prefer-dist yii2mod/yii2-link-preview "*"
or add
"yii2mod/yii2-link-preview": "*"
to the require section of your composer.json
file.
Usage
- Execute init migration:
php yii migrate/up --migrationPath=@vendor/yii2mod/yii2-link-preview/migrations
- Define preview action in your controller:
public function actions() { return [ 'link-preview' => LinkPreviewAction::className() ]; }
- Add widget to your page as follows:
echo LinkPreview::widget([ 'selector' => '#your-input-id or .someclass', 'clientOptions' => [ 'previewActionUrl' => \yii\helpers\Url::to(['link-preview']) ], ])
Example of usage with the ActiveForm and saving the page info
- Create the basic form in the view:
<?php $form = \yii\widgets\ActiveForm::begin() ?> <div class="form-group"> <label for="preview">Preview</label> <input name="preview" class="form-control" id="preview" placeholder="Preview"> </div> <?php echo \yii2mod\linkpreview\LinkPreview::widget([ 'selector' => '#preview', 'clientOptions' => [ 'previewActionUrl' => \yii\helpers\Url::to(['link-preview']) ], ]) ?> <div class="form-group"> <?= \yii\helpers\Html::submitButton('Save', ['class' => 'btn btn-primary']) ?> </div> <?php \yii\widgets\ActiveForm::end() ?>
- Add the following code to your action for the saving page info:
$model = new LinkPreviewModel(); if ($model->load(Yii::$app->request->post()) && $model->validate()) { $model->save(); } // or the short version $linkPreviewId = LinkPreviewModel::saveAndGetId(Yii::$app->request->post());