public function beforeAction($action) { $model = new SearchForm(); if ($model->load(Yii::$app->request->post()) && $model->validate()) { $q = Html::encode($model->q); return $this->redirect(Yii::$app->urlManager->createUrl(['site/search', 'q' => $q])); } return true; }
/** * Lists all Books models. * @return mixed */ public function actionIndex() { $searchForm = new SearchForm(); $authors = Authors::getAuthorsForDropDownList(); Yii::$app->session->set('absoluteUrl', Yii::$app->request->absoluteUrl); if (Yii::$app->request->isPost && $searchForm->validate()) { $searchForm->load(Yii::$app->request->post()); Yii::$app->session->set('searchForm', $searchForm); } else { if (Yii::$app->session->has('searchForm')) { $searchForm = Yii::$app->session->get('searchForm'); } } $query = $searchForm->getSearchQuery(); $dataProvider = new ActiveDataProvider(['query' => $query->with('author'), 'pagination' => ['pagesize' => 10]]); return $this->render('index', ['dataProvider' => $dataProvider, 'searchForm' => $searchForm, 'authors' => $authors]); }
public function actionBooks() { $session = Yii::$app->session; $search = new SearchForm(); if ($search->load(Yii::$app->request->post()) && $search->validate()) { $session['search'] = Yii::$app->request->post(); } elseif (!empty($session['search']['SearchForm'])) { $search->load($session['search']); } if (empty($session['search']['SearchForm'])) { $books = Books::find()->all(); } else { $books = Books::findWithFilters($session['search']['SearchForm']); } $authors = Authors::find()->all(); $selected_authors = array(); foreach ($authors as $author) { $selected_authors[$author->id] = $author['firstname'] . " " . $author['lastname']; } return $this->render('books', ['model' => $search, 'authors' => $selected_authors, 'books' => $books]); }