yii2-wavecms-news
News module for Yii 2 WaveCMS., (*1)
Please do all install steps first from Yii 2 WaveCMS., (*2)
Installation
The preferred way to install this extension is through composer., (*3)
Run, (*4)
composer require --prefer-source "mrstroz/yii2-wavecms-news" "~0.4.0"
or add, (*5)
"mrstroz/yii2-wavecms-news": "~0.4.0"
to the require section of your composer.json
file., (*6)
Required
- Update
backend/config/main.php
(Yii2 advanced template)
'modules' => [
// ...
'wavecms-news' => [
'class' => 'mrstroz\wavecms\news\Module',
/*
* Override classes
'classMap' => [
'News' => 'common\models\News',
]
*/
],
],
Form views can be overwritten by backend themes;, (*7)
- Update
frontend/config/main.php
(Yii2 advanced template)
'modules' => [
'sitemap' => [
'class' => 'himiklab\sitemap\Sitemap',
'models' => [
'mrstroz\wavecms\news\models\News',
],
'urls' => [
[
'loc' => ['/'],
'changefreq' => \himiklab\sitemap\behaviors\SitemapBehavior::CHANGEFREQ_DAILY,
'priority' => 1,
]
],
'cacheExpire' => 1
]
],
// ...
'components' => [
'urlManager' => [
'rules' => [
// Add rule for sitemap.xml
['pattern' => 'sitemap', 'route' => 'sitemap/default/index', 'suffix' => '.xml'],
],
],
]
- Run migration
Add the migrationPath
in console/config/main.php
and run yii migrate
:, (*8)
// Add migrationPaths to console config:
'controllerMap' => [
'migrate' => [
'class' => 'yii\console\controllers\MigrateController',
'migrationPath' => [
'@vendor/mrstroz/yii2-wavecms-news/migrations',
],
],
],
Or run migrates directly, (*9)
yii migrate/up --migrationPath=@vendor/mrstroz/yii2-wavecms-news/migrations
Overriding classes
Classes can be overridden by:
1. classMap
attribute for WaveCMS module, (*10)
'modules' => [
// ...
'wavecms-news' => [
'class' => 'mrstroz\wavecms\news\Module',
'classMap' => [
'News' => \common\models\News::class
]
],
],
- Yii2 Dependency Injection configuration in
backend/config/main.php
'container' => [
'definitions' => [
mrstroz\wavecms\news\models\News::class => common\models\News::class
],
],
Overriding controllers
Use controllerMap
attribute for WaveCMS module to override controllers, (*11)
'modules' => [
// ...
'wavecms' => [
'class' => 'mrstroz\wavecms\news\Module',
'controllerMap' => [
'news' => 'backend\controllers\NewsController'
]
],
],
Overriding views
Use themes for override views, (*12)
'components' => [
// ...
'view' => [
'theme' => [
'basePath' => '@app/themes/basic',
'baseUrl' => '@web/themes/basic',
'pathMap' => [
'@wavecms_news/views' => '@app/themes/basic/wavecms-news',
],
],
],
// ...
],
Usage in frontend
News
- Add new rules to your urlManager. You can do it in one of your
Bootstrap
classes
<?php
use mrstroz\wavecms\news\models\News;
use Yii;
// ...
//Parse request to set language before run ActiveRecord::find()
Yii::$app->urlManager->parseRequest(Yii::$app->request);
/** @var Settings $settings */
$settings = Yii::$app->settings;
$link = $settings->get('NewsSettings_' . Yii::$app->language, 'overview_link');
$model = Yii::createObject(News::class);
Yii::$app->getUrlManager()->addRules([
$link => 'news/index'
]);
$news = $model::find()->select(['link'])->byAllCriteria()->byType(['news'])->column();
if ($news) {
Yii::$app->getUrlManager()->addRules([
$link . '/<link:(' . implode('|', $news) . ')>' => 'news/detail'
]);
}
- Get list of news on overview page
<?php
use mrstroz\wavecms\news\models\News;
use Yii;
use yii\data\Pagination;
public function actionIndex() {
/** @var Settings $settings */
$settings = Yii::$app->settings;
$query = News::find()->byAllCriteria()->byType('news')->orderBy('create_date DESC');
$count = $query->count();
$pagination = new Pagination([
'totalCount' => $count,
'pageSize' => $settings->get('NewsSettings', 'news_on_page')
]);
$news = $query->offset($pagination->offset)
->limit($pagination->limit)
->all();
return $this->render('index', [
'news' => $news,
'pagination' => $pagination
]);
}
- Display news in view 'index.php'
<?php
use yii\helpers\Html;
use yii\widgets\LinkPager;
/** @var \mrstroz\wavecms\news\models\News[] $news */
/** @var \yii\data\Pagination $pagination */
/** @var \yii2mod\settings\components\Settings $settings */
$settings = Yii::$app->settings;
$link = $settings->get('NewsSettings_' . Yii::$app->language, 'overview_link');
if ($news) {
foreach ($news as $oneNews) {
echo Html::a($oneNews->title, [$link . '/' . $oneNews->link]);
echo '<br />';
}
}
echo LinkPager::widget([
'pagination' => $pagination,
]);
- Get requested news by link in
detail
action
<?php
use mrstroz\wavecms\news\models\News;
// ...
public function actionDetail($link)
{
$news = News::find()->getByLink($link)->one();
return $this->render('detail', [
'news' => $news
]);
}
See yii2-wavecms-metatags, (*13)
Add news to sitemap
According to Sitemap module, we need to add behaviour to our AR model and then add model to sitemap module configuration (see frontend/config/main.php), (*14)
use himiklab\sitemap\behaviors\SitemapBehavior;
public function behaviors()
{
return [
'sitemap' => [
'class' => SitemapBehavior::className(),
'scope' => function ($model) {
/** @var \yii\db\ActiveQuery $model */
$model->select(['url', 'lastmod']);
$model->andWhere(['is_deleted' => 0]);
},
'dataClosure' => function ($model) {
/** @var self $model */
return [
'loc' => Url::to($model->url, true),
'lastmod' => strtotime($model->lastmod),
'changefreq' => SitemapBehavior::CHANGEFREQ_DAILY,
'priority' => 0.8
];
}
],
];
}
Used packages
- CKEditor https://github.com/MihailDev/yii2-ckeditor
- ElFinder https://github.com/MihailDev/yii2-elfinder
- Slugify https://github.com/powerkernel/yii2-slugify
- Select2 https://github.com/kartik-v/yii2-widget-select2
- Datepicker https://github.com/kartik-v/yii2-widget-datepicker
- Switch widget https://github.com/2amigos/yii2-switch-widget
- Sitemap - https://github.com/himiklab/yii2-sitemap-module
INWAVE - Internet Software House
inwave.eu, (*15)