Skip to content

Commit

Permalink
support for other formats
Browse files Browse the repository at this point in the history
  • Loading branch information
glpzzz committed Mar 8, 2021
1 parent 2f0c2a7 commit c2ff2ff
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 7 deletions.
1 change: 1 addition & 0 deletions Bootstrap.php
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ public function bootstrap($app)
$langs = join('|', $module->languages);
$module->urlRules = [
'<_lang:(' . $langs . ')>' => 'page/home',
'<_lang:(' . $langs . ')>/<slug:[\w/-]+>.<_format>' => 'page/view',
'<_lang:(' . $langs . ')>/<slug:[\w/-]+>' => 'page/view',
];
}
Expand Down
11 changes: 8 additions & 3 deletions controllers/frontend/PageController.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ public function behaviors()
'formats' => [
'text/html' => Response::FORMAT_HTML,
'application/json' => Response::FORMAT_JSON,
'application/xml' => Response::FORMAT_XML,
],
'languages' => $this->module->languages,
],
Expand All @@ -38,7 +39,7 @@ public function actionHome($_lang = null)
return $this->actionView($homePage->slug, $_lang);
}

public function actionView($slug, $_lang)
public function actionView($slug, $_lang, $_format = Response::FORMAT_HTML)
{
$model = $this->findModel($slug, $_lang);
$dataProvider = new ActiveDataProvider([
Expand All @@ -47,10 +48,14 @@ public function actionView($slug, $_lang)
->orderBy($model->type->sort_by),
]);

return $this->render($this->getViewFile($model), [
$data = [
'model' => $model,
'dataProvider' => $dataProvider,
]);
];

return $_format == Response::FORMAT_HTML
? $this->render($this->getViewFile($model), $data)
: $data;
}

}
16 changes: 13 additions & 3 deletions models/Metadata.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,10 +58,10 @@ public function customValidator($attribute, $params)
public function attributeLabels()
{
return [
'id' => Yii::t('website','ID'),
'id' => Yii::t('website', 'ID'),
'value' => $this->metadataDefinition->name,
'page_id' => Yii::t('website','Page ID'),
'metadata_definition_id' => Yii::t('website','Metadata Definition ID'),
'page_id' => Yii::t('website', 'Page ID'),
'metadata_definition_id' => Yii::t('website', 'Metadata Definition ID'),
];
}

Expand All @@ -81,6 +81,16 @@ public function getPage()
return $this->hasOne(Page::className(), ['id' => 'page_id']);
}

public function fields()
{
return [
'name' => function () {
return $this->metadataDefinition->name;
},
'value'
];
}

/**
* {@inheritdoc}
* @return MetadataQuery the active query used by this AR class.
Expand Down
3 changes: 2 additions & 1 deletion models/Page.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,6 @@ public function rules()
[['body'], 'cleanHtmlValidator'],
[['title', 'slug', 'abstract', 'language'], 'string', 'max' => 1000],
[['image', 'abstract', 'body'], 'default'],
[['image'], 'url'],
[['status'], 'integer'],
[['status'], 'default', 'value' => self::STATUS_POST_DRAFT],
[['parent_id'], 'exist', 'skipOnError' => true, 'targetClass' => Page::class, 'targetAttribute' => ['parent_id' => 'id']],
Expand Down Expand Up @@ -198,6 +197,8 @@ public function fields()
},
'created_at',
'updated_at',
'metadatas',
'pages',
];
}

Expand Down

0 comments on commit c2ff2ff

Please sign in to comment.