1. Go to this page and download the library: Download vrtc/yii2-export library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
vrtc / yii2-export example snippets
// в файле настройки приложения (main.php - Advanced или web.php - Basic) добавляется класс в controllerMap
...
'controllerMap' => [
'export' => 'vrtc\exportFile\controllers\ExportController'
],
'components' => [
...
],
...
class GeoCitySearch extends GeoCity
{
...
// указываются свойства, которые нужно выводить в файлы
public function exportFields()
{
return [
'id' => function ($model) {
/* @var $model User */
return $model->id;
},
'name_ru',
'region_id' => function ($model) {
/* @var $model GeoCity */
if (isset($model->region->name_ru)) {
return $model->region->name_ru;
}
return false;
},
'lat',
'lon'
];
}
...
}
...
// cоздается стандартное действие для вывода данных
public function actionExportFile()
{
$searchModel = new GeoCitySearch();
$dataProvider = $searchModel->search(\Yii::$app->request->queryParams);
return $this->render('export-file', [
'searchModel' => $searchModel,
'dataProvider' => $dataProvider,
]);
}
...
use vrtc\exportFile\ExportFile;
use yii\grid\GridView;
/* @var $searchModel \common\models\GeoCitySearch */
/* @var $dataProvider yii\data\ActiveDataProvider */
// минимальные настройки
echo ExportFile::widget([
'model' => 'common\models\GeoCitySearch', // путь к модели
'searchAttributes' => $searchModel, // фильтр
])