You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

113 lines
2.6 KiB

7 years ago
<?php
namespace backend\controllers;
use core\entities\Search;
use core\forms\SearchForm;
7 years ago
use Yii;
use yii\data\ActiveDataProvider;
7 years ago
use yii\web\Controller;
use yii\filters\VerbFilter;
use yii\filters\AccessControl;
use common\models\LoginForm;
/**
* Site controller
*/
class SiteController extends Controller
{
/**
* @inheritdoc
*/
public function behaviors()
{
return [
'access' => [
'class' => AccessControl::class,
7 years ago
'rules' => [
[
'actions' => ['error'],
'allow' => true,
],
[
'actions' => ['index', 'search'],
7 years ago
'allow' => true,
'roles' => ['Dashboard'],
],
[ // all the action are accessible to admin
'allow' => true,
'roles' => ['admin'],
],
],
],
'verbs' => [
'class' => VerbFilter::class,
7 years ago
'actions' => [
'logout' => ['post'],
],
],
];
}
/**
* @inheritdoc
*/
public function actions()
{
return [
'error' => [
'class' => 'yii\web\ErrorAction',
],
];
}
/**
* Displays homepage.
*
* @return string
*/
public function actionIndex()
{
return $this->render('index');
}
public function actionSearch()
{
$form = new SearchForm();
if ($form->load(Yii::$app->request->post()) && $form->validate()) {
try {
$query = Search::find()
->andWhere(['LIKE', 'title', $form->query])
->orWhere(['LIKE', 'content', $form->query]);
$provider = new ActiveDataProvider([
'query' => $query,
'pagination' => [
'pageSize' => 10,
],
'sort' => [],
]);
return $this->render('search', [
'provider' => $provider,
'form' => $form
]);
//$page = $this->service->create($form);
//return $this->redirect(['view', 'id' => $page->id]);
} catch (\DomainException $e) {
//Yii::$app->errorHandler->logException($e);
//Yii::$app->session->setFlash('error', $e->getMessage());
}
}
}
7 years ago
public function beforeAction($action)
{
if ($action->id === 'error') {
$this->layout = 'error';
}
return parent::beforeAction($action);
}
}