Display image in grid view YII2

In Yii 2 there are many function including built-in helper for building urls. So we can easily bulld the url to image by path too .

To display the image in grid view :

[php]
[
‘attribute’ => ‘image’,
‘format’ => ‘html’,
‘value’ => function($data) {
return Html::img(\Yii::$app->request->BaseUrl.’/uploads/’.$data->image,[‘width’=>100]);
},
]
[/php]

To display the image in DetailView :
[php]
[
‘attribute’=>’image’,
‘value’=>(\Yii::$app->request->BaseUrl.’/uploads/tips/’.$model->image),
//’value’=>$model->image,
‘format’ => [‘image’,[‘width’=>’100′,’height’=>’100’]],
],
[/php]

Install yii2 advanced template using composer in windows

The minimum requirement for YII2 is that your Web server supports PHP 5.4.0.

The installation require following steps:

1: If you don’t have composer, download it from https://getcomposer.org/Composer-Setup.exe and execulete in yout root folder like c:\xampp\php\
2: Go to your project folder like c:\xampp\htdocs\yourprojectfolder
[php]
> c:\xampp\htdocs\yourprojectfolder>composer global require "fxp/composer-asset-plugin:^1.2.0"
> c:\xampp\htdocs\yourprojectfolder>composer create-project –prefer-dist –stability=dev yiisoft/yii2-app-advanced[/php]

3. After this run php init command it is just copy files from directory /environments to your project
[php]> c:\xampp\htdocs\yourprojectfolder>php init[/php]

Excel reader in YII2

PHPExcel extension is used to experting and importing Excel to PHP. In this example we are reading Excel data by using this extension and inserting the data in given tables accordingly.

PHPExcel extension is used to experting and importing Excel to PHP. In this example we are reading Excel data by using this extension and inserting the data in given tables accordingly.

Step 1: Installation : Download the extension from https://github.com/PHPOffice/PHPExcel and put it in vendor directory. Then update your composer or add the following code in your composer.jason file under require array().
code: “phpoffice/phpexcel”: “dev-develop” Download the extension from https://github.com/PHPOffice/PHPExcel and put it in vendor directory. Then update your composer or add the following code in your composer.jason file under require array().
[php]code: "phpoffice/phpexcel": "dev-develop"[/php]
step 2: In your model :
[php]public $files;
public static function tableName()
{
return ‘tbl’;
}

/**
* @inheritdoc
*/
public function rules()
{
return [
[[‘files’], ‘file’, ‘skipOnEmpty’ => true, ‘extensions’ => ‘csv, xlsx,xls’, ‘maxFiles’ => 1],
];
}
[/php]
Step 3: In your controller
[php] public function actionImport()
{
$objPHPExcel = new \PHPExcel();
if (Yii::$app->request->isPost) {
$excel_files = UploadedFile::getInstance($model, ‘files’);
$objPHPExcel = \PHPExcel_IOFactory::load($excel_files->tempName);
$sheetData = $objPHPExcel->getActiveSheet()->toArray(null, true, true, true);
if(isset($sheetData)){
foreach($sheetData as $row){
/*Your code*/
}
}
}
}
[/php]
Step 3: In your View
[php]<?php $form = ActiveForm::begin([‘options’ => [‘enctype’ => ‘multipart/form-data’]]) ?>
<?= $form->field($model, ‘files’)->fileInput() ?>
<?php echo Html::submitButton($model->isNewRecord ? ‘Upload’ : ‘Update’, [
‘class’=>$model->isNewRecord ? ‘btn btn-success’ : ‘btn btn-primary’]
);?>
<?php ActiveForm::end() ?>
[/php]

DataGridView Text Wrap in Yii2 framework

In Yii2 by using “contentOptions” we can easily warp the text
[php]
<?= GridView::widget([
‘dataProvider’ => $dataProvider,
‘filterModel’ => $searchModel,
‘columns’ => [
[‘class’ => ‘yii\grid\SerialColumn’],

//’id’,
[
‘attribute’ => ‘question’,
‘value’=>’question’,
‘contentOptions’=>[‘style’=> ‘max-width: 300px;’, ‘height: 100px’]
],
]); ?>
[/php]