I am using this piece of code, and try using REST API in yii2. I tried to use two function as you seen in the code snippet.
getSpecificData
<?php namespace app\\api\\modules\\widgetmodule\\controllers; use yii\\rest\\Controller; class WidgetController extends Controller { public $modelClass = 'app\\models\\DynamicWidget'; public function actions() { return [ 'index' => [ 'class' => 'yii\\rest\\IndexAction', 'modelClass' => $this->modelClass, 'prepareDataProvider' => [$this, 'getAllData'] ], 'view' => [ 'class' => 'yii\\rest\\ViewAction', 'modelClass' => $this->modelClass, 'prepareDataProvider' => [$this, 'getSpecificData'] ], ]; } public function getAllData() { die('get all data'); } public function getSpecificData() { die('get specific data'); } }
I tried two URLs for the two different methods,
http://localhost/api/web/widgetmodule/widget/getAllData
http://localhost/api/web/widgetmodule/widget/getSpecificData
But the output will always be like.
Quote get all data
Here is my URL manager code in api.php
'urlManager' => [
'enablePrettyUrl' => true,
'enableStrictParsing' => true,
'showScriptName' => false,
'rules' => [
'<controller:\w+>/<action:\w+>/<id:\w+>' => '<controller>/<action>',
'<controller:\w+>/<action:\w+>' => '<controller>/<action>',
[
'class' => 'yii\rest\UrlRule',
'controller' => ['widgetmodule/widget']
]
],
],
'db' => $db,
],
'modules' => [
'widgetmodule' => [
'class' => 'app\api\modules\widgetmodule\Module',
],
So could anyone help me, how to get different outputs with two different methods.
It is anyways calling the first method.
Any help will be appreciated.
Thanks in advance.
1- The only 2 actions you did define inside your controller are index
and view
and because index is rendering getAllData
it is why you are getting that output.
2- the view
action has no prepareDataProvider
attribute.
maybe you meant something like this instead:
public function actions()
{
return [
'index' => [
'class' => 'yii\rest\IndexAction',
'modelClass' => $this->modelClass,
'prepareDataProvider' => [$this, 'getAllData']
],
'view' => [
'class' => 'yii\rest\ViewAction',
'modelClass' => $this->modelClass,
],
];
}
public function getAllData()
{
// return some dataProvider instance to be used by index
$modelClass = $this->modelClass;
return new ActiveDataProvider([
'query' => $modelClass::find(),
]);
}
And that is defining two actions: index
and view
where index's dataProvider is overriden by a custom function getAllData()
. that requires something similar to those configs:
[
'class' => 'yii\rest\UrlRule',
'controller' => ['widgetmodule/widget'],
'patterns' => [
'GET,HEAD index' => 'index',
'GET,HEAD view/{id}' => 'view',
]
],
So you can access them within the following endpoints:
http://localhost/api/web/widgetmodule/widget/index
http://localhost/api/web/widgetmodule/widget/view/4
Now if your question is how to add an extra action to that in order to respond to the uri http://localhost/api/web/widgetmodule/widget/getSpecificData
then you'll need to add this to your patterns:
'patterns' => [
...
...
'GET getSpecificData' => 'some-specific-data',
]
and define that action inside your controller as described in official docs by simply adding this:
public function actionSomeSpecificData()
{
return 'some specific data';
}
First unset default index and view actions, like this:
public function actions()
{
$actions = parent::actions();
// unset default index action for custom our own code
unset($actions['index']);
unset($actions['view']);
return ArrayHelper::merge($actions, [
'index' => [
'class' => 'yii\rest\IndexAction',
'modelClass' => $this->modelClass,
'prepareDataProvider' => [$this, 'getAllData']
],
'view' => [
'class' => 'yii\rest\ViewAction',
'modelClass' => $this->modelClass,
'prepareDataProvider' => [$this, 'getSpecificData']
],
];
}
Config routes like this:
[
'class' => 'yii\rest\UrlRule',
'controller' => ['widgetmodule/widget'],
'patterns' => [
'GET,HEAD index' => 'index',
'GET,HEAD view/{id}' => 'view',
]
],
Then you can call these urls:
http://localhost/api/web/widgetmodule/widget/index
http://localhost/api/web/widgetmodule/widget/view/4
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.