Skip to content

Commit cbb3aac

Browse files
committed
Add an option to decide to use $_POST or $_GET values depending on the request method
1 parent 02df094 commit cbb3aac

File tree

1 file changed

+20
-6
lines changed

1 file changed

+20
-6
lines changed

src/DataTableAction.php

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,13 @@
2222
*/
2323
class DataTableAction extends Action
2424
{
25+
/**
26+
* GET or POST
27+
*
28+
* @var string
29+
*/
30+
public $requestMethod = "GET";
31+
2532
/**
2633
* @var ActiveQuery
2734
*/
@@ -50,24 +57,31 @@ public function init()
5057
}
5158
}
5259

60+
protected function getParam($name, $defaultValue = null)
61+
{
62+
return $this->requestMethod == 'GET' ?
63+
Yii::$app->request->getQueryParam($name, $defaultValue) :
64+
Yii::$app->request->getBodyParam($name, $defaultValue);
65+
}
66+
5367
public function run()
5468
{
5569
/** @var ActiveQuery $originalQuery */
5670
$originalQuery = $this->query;
5771
$filterQuery = clone $originalQuery;
58-
$draw = Yii::$app->request->getQueryParam('draw');
72+
$draw = $this->getParam('draw');
5973
$filterQuery->where = null;
60-
$search = Yii::$app->request->getQueryParam('search', ['value' => null, 'regex' => false]);
61-
$columns = Yii::$app->request->getQueryParam('columns', []);
62-
$order = Yii::$app->request->getQueryParam('order', []);
74+
$search = $this->getParam('search', ['value' => null, 'regex' => false]);
75+
$columns = $this->getParam('columns', []);
76+
$order = $this->getParam('order', []);
6377
$filterQuery = $this->applyFilter($filterQuery, $columns, $search);
6478
$filterQuery = $this->applyOrder($filterQuery, $columns, $order);
6579
if (!empty($originalQuery->where)) {
6680
$filterQuery->andWhere($originalQuery->where);
6781
}
6882
$filterQuery
69-
->offset(Yii::$app->request->getQueryParam('start', 0))
70-
->limit(Yii::$app->request->getQueryParam('length', -1));
83+
->offset($this->getParam('start', 0))
84+
->limit($this->getParam('length', -1));
7185
$dataProvider = new ActiveDataProvider(['query' => $filterQuery, 'pagination' => ['pageSize' => Yii::$app->request->getQueryParam('length', 10)]]);
7286
Yii::$app->response->format = Response::FORMAT_JSON;
7387
try {

0 commit comments

Comments
 (0)