Hi Guys,
I’m creating rest api’s for my system and I was able to create the index and view api by doing this in my DriverControllers and it worked:
public function actions()
{
return [
'index' => [
'class' => 'yii\rest\IndexAction',
'modelClass' => $this->modelClass,
'prepareDataProvider' => [$this, 'prepareDataProvider']
],
'view' => [
'class' => 'yii\rest\ViewAction',
'modelClass' => $this->modelClass,
'findModel' => [$this, 'findModel']
],
'options' => [
'class' => 'yii\rest\OptionsAction'
]
];
}
public function prepareDataProvider()
{
$query = Driver::find();
$dataProvider = new ActiveDataProvider([
'query' => $query,
'pagination' => false,
]);
return $dataProvider;
}
public function findModel($id)
{
$model = Driver::find()
->where(['driver_id' => (int) $id])
->one();
if (!$model) {
throw new HttpException(404);
}
return $model;
}
My question is, how can I do the create action? I want to be able to add a new driver via the API.
Thanks for your help. I love this community.
Have you tried simply with:
public function actionCreate()
{
...
...
...
}
I tried but this is what I get: https://www.dropbox.com/s/zdmsfhd5cwmwv00/Screenshot%202016-03-23%2015.44.38.png?dl=0
<response><name>Method Not Allowed</name><message>Method Not Allowed. This url can only handle the following request methods: POST.</message><code>0</code><status>405</status><type>yii\web\MethodNotAllowedHttpException</type></response>
Hi Guys,
I’m creating rest api’s for my system and I was able to create the index and view api by doing this in my DriverControllers and it worked:
public function actions()
{
return [
'index' => [
'class' => 'yii\rest\IndexAction',
'modelClass' => $this->modelClass,
'prepareDataProvider' => [$this, 'prepareDataProvider']
],
'view' => [
'class' => 'yii\rest\ViewAction',
'modelClass' => $this->modelClass,
'findModel' => [$this, 'findModel']
],
'options' => [
'class' => 'yii\rest\OptionsAction'
]
];
}
public function prepareDataProvider()
{
$query = Driver::find();
$dataProvider = new ActiveDataProvider([
'query' => $query,
'pagination' => false,
]);
return $dataProvider;
}
public function findModel($id)
{
$model = Driver::find()
->where(['driver_id' => (int) $id])
->one();
if (!$model) {
throw new HttpException(404);
}
return $model;
}
My question is, how can I do the create action? I want to be able to add a new driver via the API.
I already tried to create a public function actionCreate but I get this error: Method Not Allowed - https://www.dropbox.com/s/zdmsfhd5cwmwv00/Screenshot%202016-03-23%2015.44.38.png?dl=0
Thanks for your help. I love this community.
If you read carefully the answer, you need to call that action using POST method.
It worked now thanks. I was overriding the method with something else.
DO you know how can I verify if the user is authenticated? I saw yii tutorials but I don’t know how to get the token.
You have some way to authenticate the user.
This is a good starting point:
http://www.yiiframework.com/doc-2.0/guide-rest-authentication.html
softark
(Softark)
March 23, 2016, 9:36pm
8
[color="#006400 "]/* Moved from "Tips" to "REST APIs" */[/color]