/** * Main search method * @return string * @throws \Ffcms\Core\Exception\NativeException * @throws NotFoundException * @throws \Ffcms\Core\Exception\SyntaxException */ public function actionIndex() { // get search query value from GET headers $query = (string) $this->request->query->get('query', null); // strip html tags $query = App::$Security->strip_tags(trim($query)); // get configs $configs = $this->getConfigs(); // check search query length if (Str::likeEmpty($query) || Str::length($query) < (int) $configs['minLength']) { throw new NotFoundException(__('Search query is too short!')); } // prevent sh@t query's with big length if (Str::length($query) > static::QUERY_MAX_LENGTH) { throw new NotFoundException(__('Search query is too long!')); } // initialize search controller model $model = new EntitySearchMain($query, $configs); // try to search by default apps $model->make(); // register search event to allow extend it model results App::$Event->run(static::EVENT_SEARCH_RUNNED, ['model' => $model]); // render output view with search result return $this->view->render('index', ['model' => $model, 'query' => $query]); }
/** * Print json response for search query based on standard model * @return string * @throws JsonException */ public function actionIndex() { $this->setJsonHeader(); // get search query as string from request $query = $this->request->query->get('query', null); if (Str::likeEmpty($query) || Str::length($query) < 2) { throw new JsonException('Short query'); } // initialize basic search model $model = new EntitySearchMain($query, ['itemPerApp' => 3]); $model->make(); // build response by relevance as array $response = $model->getRelevanceSortedResult(); return json_encode(['status' => 1, 'count' => count($response), 'data' => $response]); }