mrstroz / yii2-wavecms-news
News module for WaveCMS
Installs: 69
Dependents: 1
Suggesters: 0
Security: 0
Stars: 0
Watchers: 1
Forks: 0
Open Issues: 0
Type:yii2-extension
Requires
- php: >=5.6.0
- 2amigos/yii2-switch-widget: ~1.0
- himiklab/yii2-sitemap-module: *
- kartik-v/yii2-widget-datepicker: @dev
- mrstroz/yii2-wavecms: *
- mrstroz/yii2-wavecms-metatags: ~0.2.0
- powerkernel/yii2-slugify: *
- yiisoft/yii2: ~2.0.6
Requires (Dev)
- php: >=5.4.0
- yiisoft/yii2: ~2.0.6
README
News module for Yii 2 WaveCMS.
Please do all install steps first from Yii 2 WaveCMS.
Installation
The preferred way to install this extension is through composer.
Run
composer require --prefer-source "mrstroz/yii2-wavecms-news" "~0.4.0"
or add
"mrstroz/yii2-wavecms-news": "~0.4.0"
to the require section of your composer.json
file.
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;
- 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
:
// Add migrationPaths to console config: 'controllerMap' => [ 'migrate' => [ 'class' => 'yii\console\controllers\MigrateController', 'migrationPath' => [ '@vendor/mrstroz/yii2-wavecms-news/migrations', ], ], ],
Or run migrates directly
yii migrate/up --migrationPath=@vendor/mrstroz/yii2-wavecms-news/migrations
Overriding classes
Classes can be overridden by:
classMap
attribute for WaveCMS module
'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
'modules' => [ // ... 'wavecms' => [ 'class' => 'mrstroz\wavecms\news\Module', 'controllerMap' => [ 'news' => 'backend\controllers\NewsController' ] ], ],
Overriding views
Use themes for override views
'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 ]); }
Meta tags
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)
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