protected function findModelBySlug($slug) { if (($model = Post::findOne(['slug' => $slug])) !== null) { return $model; } else { throw new NotFoundHttpException(); } }
/** * Finds the Post model based on its primary key value. * If the model is not found, a 404 HTTP exception will be thrown. * @param integer $id * @return Post the loaded model * @throws NotFoundHttpException if the model cannot be found */ protected function findModel($id) { if (($model = Post::findOne($id)) !== null) { return $model; } else { throw new NotFoundHttpException('The requested post does not exist.'); } }
/** * Creates data provider instance with search query applied * * @param array $params * * @return ActiveDataProvider */ public function search($params) { $query = Post::find(); // add conditions that should always apply here $dataProvider = new ActiveDataProvider(['query' => $query, 'sort' => ['defaultOrder' => ['updated_at' => 3]]]); $this->load($params); if (!$this->validate()) { // uncomment the following line if you do not want to return any records when validation fails // $query->where('0=1'); return $dataProvider; } // grid filtering conditions $query->andFilterWhere(['id' => $this->id, 'created_at' => $this->created_at, 'updated_at' => $this->updated_at, 'status' => $this->status]); $query->andFilterWhere(['like', 'title', $this->title])->andFilterWhere(['like', 'body', $this->body])->andFilterWhere(['like', 'slug', $this->slug])->andFilterWhere(['like', 'keywords', $this->keywords])->andFilterWhere(['like', 'description', $this->description]); return $dataProvider; }
public function actionSitemap() { if (Yii::$app->request->isPost) { $sitemap = new Sitemap(Yii::getAlias('@webroot/sitemap.xml')); $sitemap->addItem(Url::to(['/main/default/index'], true), time(), Sitemap::WEEKLY, 1); $sitemap->addItem(Url::to(['/main/contact/index'], true), time(), Sitemap::MONTHLY, 1); $pages = Page::findAll(['status' => 1]); foreach ($pages as $page) { $sitemap->addItem(Url::to(['/page/node/view', 'slug' => $page->slug], true), $page->updated_at, Sitemap::MONTHLY, 1); } $posts = Post::findAll(['status' => 1]); foreach ($posts as $post) { $sitemap->addItem(Url::to(['/post/node/view', 'slug' => $post->slug], true), $post->updated_at, Sitemap::NEVER, 0.8); } $sitemap->write(); Yii::$app->session->setFlash('success', "Файл sitemap.xml успешно обновлен."); Yii::$app->request->referrer ? $this->redirect(Yii::$app->request->referrer) : $this->goHome(); } else { throw new NotFoundHttpException('Страница не найдена'); } }
public function actionIndex() { $dataPages = new ActiveDataProvider(['query' => Post::find()->where(['status' => 1])->orderBy('updated_at DESC'), 'pagination' => ['pageSize' => 10]]); return $this->render('index', ['dataPages' => $dataPages]); }
use yii\widgets\Pjax; use app\modules\post\models\backend\Post; use app\widgets\grid\SetColumn; /* @var $this yii\web\View */ /* @var $searchModel app\modules\post\models\backend\search\PostSearch */ /* @var $dataProvider yii\data\ActiveDataProvider */ $this->title = 'Новости'; $this->params['breadcrumbs'][] = $this->title; ?> <h1><?php echo Html::encode($this->title); ?> </h1> <p class="pull-left"> <?php echo Html::a('<i class="material-icons">add</i> Добавить новость', ['create'], ['class' => 'btn btn-success']); ?> </p> <?php Pjax::begin(['enablePushState' => false]); echo GridView::widget(['dataProvider' => $dataProvider, 'filterModel' => $searchModel, 'columns' => [['class' => 'yii\\grid\\CheckboxColumn', 'contentOptions' => ['class' => 'check-column']], ['attribute' => 'title', 'format' => 'raw', 'value' => function ($model, $key, $index, $column) { /** @var \app\modules\post\models\backend\Post $model */ return Html::a(Html::encode($model->title), ['/post/node/view', 'slug' => $model->slug]); }], ['attribute' => 'updated_at', 'format' => 'datetime', 'contentOptions' => ['class' => 'at-column']], ['class' => SetColumn::className(), 'filter' => Post::getStatusesArray(), 'attribute' => 'status', 'name' => 'statusName', 'cssCLasses' => [Post::STATUS_ACTIVE => 'success', Post::STATUS_WAIT => 'warning'], 'contentOptions' => ['class' => 'status-column']], ['class' => 'app\\widgets\\grid\\ActionColumn', 'buttons' => ['view' => function ($url, $model, $key) { /** @var \app\modules\post\models\backend\Post $model */ return Html::a('<i class="material-icons">visibility</i>', ['/post/node/view', 'slug' => $model->slug]); }]]]]); Pjax::end();