/** * Creates data provider instance with search query applied * * @param array $params * * @return ActiveDataProvider */ public function search($params) { $query = Slide::find(); $dataProvider = new ActiveDataProvider(['query' => $query, 'sort' => ['defaultOrder' => ['sortOrder' => SORT_ASC]]]); $this->load($params); if (!$this->validate()) { // uncomment the following line if you do not want to any records when validation fails // $query->where('0=1'); return $dataProvider; } $query->andFilterWhere(['id' => $this->id, 'sortOrder' => $this->sortOrder]); $query->andFilterWhere(['like', 'title', $this->title])->andFilterWhere(['like', 'body', $this->body]); return $dataProvider; }
<?php use yii\bootstrap\Carousel; use yii\helpers\Html; use yii\widgets\ListView; use common\models\Slide; /* @var $this yii\web\View */ /* @var $dataProvider yii\data\ActiveDataProvider */ $this->title = Yii::t('frontend/site', 'Home'); $slides = []; foreach (Slide::find()->orderBy('sortOrder')->all() as $slide) { /** @var $slide common\models\Slide */ $slides[] = ['content' => Html::img(Yii::$app->urlManager->baseUrl . '/uploads/slide/' . $slide->id . '.jpg'), 'caption' => Html::tag('h1', $slide->title) . $slide->body]; } ?> <?php echo Carousel::widget(['id' => 'home-slider', 'items' => $slides, 'options' => ['class' => 'slide'], 'controls' => ['<span class="icon-prev"></span>', '<span class="icon-next"></span>']]); ?> <div class="container"> <h1 class="page-header text-center"> <?php echo Yii::t('frontend/site', 'Last products'); ?> </h1> <?php echo ListView::widget(['layout' => "<div class=\"row\">{items}</div>", 'dataProvider' => $dataProvider, 'itemView' => '/catalog/_product', 'viewParams' => ['class' => 'col-lg-3 col-md-3 col-sm-4 col-xs-6'], 'summaryOptions' => ['class' => 'alert alert-info']]); ?>
/** * Finds the Slide model based on its primary key value. * If the model is not found, a 404 HTTP exception will be thrown. * @param integer $id * @return Slide the loaded model * @throws NotFoundHttpException if the model cannot be found */ protected function findModel($id) { if (($model = Slide::findOne($id)) !== null) { return $model; } else { throw new NotFoundHttpException('The requested page does not exist.'); } }