Answer the question
In order to leave comments, you need to log in
How to form a query with a filter in rest yii?
There is a controller Product
namespace app\controllers;
use yii\rest\ActiveController;
class ProductController extends ActiveController
{
public $modelClass = 'app\models\Product';
}
GET /products getting goods
GET /product/{id} getting goods
How to create a query: list of user's goods?
In urlmanager I created a rule to process my request which will call my action.
GET /products/user/{id} - passes but I don't know how to implement the action correctly
Answer the question
In order to leave comments, you need to log in
yii\rest\IndexAction has a 'prepareDataProvider' method. If overridden, you can use the get parameters in the query to make a fetch. For example like this:
public function actions()
{
return [
'index' => [
'class' => IndexAction::className(),
'modelClass' => $this->modelClass,
'checkAccess' => [$this, 'checkAccess'],
'prepareDataProvider' => function ($action) {
$model = new Product();
$model->load(\Yii::$app->request->queryParams)
return $model->search();
}
],
];
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question