Welcome, Guest: Register On Nairaland / LOGIN! / Trending / Recent / New
Stats: 3,152,823 members, 7,817,399 topics. Date: Saturday, 04 May 2024 at 11:32 AM

Yii2 Rest API Question - Programming - Nairaland

Nairaland Forum / Science/Technology / Programming / Yii2 Rest API Question (2539 Views)

[php Tutorial] - Building A Rest Api / How To Develop Android And iOS Mobile App In Magento2 With Rest API / Yii2 Development Visualization (2) (3) (4)

(1) (Reply) (Go Down)

Yii2 Rest API Question by nollyj: 12:31am On May 13, 2016
According to Yii 2 Rest APi documentation, I have a CountriesController that extends \yii\rest\ActiveController and a corresponding Countries model. This is the code for my Controller class.


<?php

namespace app\controllers;

class CountriesController extends \yii\rest\ActiveController{

public $modelClass = 'app\models\Countries';

public function actionIndex(){

}

public function actionView(){

}

public function actionCreate(){

}

public function actionUpdate(){

}

public function actionDelete(){

}

public function actionOptions(){

}

}


When I send a get request, it returns all the countries in my database.

My Question

is it possible to return my own result from action methods. Like in the actionIndex(), I will like to limit the result to 20 records. I did something like this but it is not working.

 public function actionIndex(){

$model = Countries::find()->one();
return $this->render($model);
}


I know that I can get all the countries from my database and loop through it and obtain only 20 results but I want to just query for 20 records from database.

@pcguru1, @dhtml and other Yii2 gurus
Re: Yii2 Rest API Question by Nobody: 7:39am On May 13, 2016
nollyj:
According to Yii 2 Rest APi documentation, I have a CountriesController that extends \yii\rest\ActiveController and a corresponding Countries model. This is the code for my Controller class.


<?php

namespace app\controllers;

class CountriesController extends \yii\rest\ActiveController{

public $modelClass = 'app\models\Countries';

public function actionIndex(){

}

public function actionView(){

}

public function actionCreate(){

}

public function actionUpdate(){

}

public function actionDelete(){

}

public function actionOptions(){

}

}


When I send a get request, it returns all the countries in my database.

My Question

is it possible to return my own result from action methods. Like in the actionIndex(), I will like to limit the result to 20 records. I did something like this but it is not working.

 public function actionIndex(){

$model = Countries::find()->one();
return $this->render($model);
}


I know that I can get all the countries from my database and loop through it and obtain only 20 results but I want to just query for 20 records from database.

@pcguru1, @dhtml and other Yii2 gurus
After the find why not call limit and pass the 20 as argument , also you can use a Cdbcriteria I will check the documentation once I reach office.
Re: Yii2 Rest API Question by tosinhtml: 9:40am On May 13, 2016
well, as far as i can remember, i use findAll instead of findOne

$model=new Countries;

$criteria = new CDbCriteria();
$count=Countries::model()->count($criteria);
$pages=new CPagination($count);

//Optional - if you choose to add conditions if column value is greater than another value
$example_value = //value
$criteria->condition = 'example_column > =:example_value';
$criteria->params = array(':example_value'=>$example_value );


// results per page
$pages->pageSize=20;
$pages->applyLimit($criteria);
$models=Countries::model()->findAll($criteria);


return $this->render('index',
array(
'models'=>$models,
'pages' => $pages,
'count' => $count
));
Re: Yii2 Rest API Question by Nobody: 12:44pm On May 13, 2016
tosinhtml:
well, as far as i can remember, i use findAll instead of findOne

$model=new Countries;

$criteria = new CDbCriteria();
$count=Countries::model()->count($criteria);
$pages=new CPagination($count);

//Optional - if you choose to add conditions if column value is greater than another value
$example_value = //value
$criteria->condition = 'example_column > =:example_value';
$criteria->params = array(':example_value'=>$example_value );


// results per page
$pages->pageSize=20;
$pages->applyLimit($criteria);
$models=Countries::model()->findAll($criteria);


return $this->render('index',
array(
'models'=>$models,
'pages' => $pages,
'count' => $count
));

OP asked for Yii2 not Yii1
Re: Yii2 Rest API Question by Nobody: 12:46pm On May 13, 2016
Countries::find()->limit(20); Should work
Re: Yii2 Rest API Question by tosinhtml: 1:04pm On May 13, 2016
pcguru1:
Countries::find()->limit(20); Should work

ok, so Yii2 totally eliminates the CDbCriteria ??
Re: Yii2 Rest API Question by nollyj: 1:14pm On May 13, 2016
Thanks all.

In Yii 2, when you controller class extends \yii\rest\ActiveController for Restful Api not app\controllers, the following methods are created like default


public function actionIndex(){

}

public function actionView(){

}

public function actionCreate(){

}

public function actionUpdate(){

}

public function actionDelete(){

}

public function actionOptions(){

}


This will map to the corresponding API end-points

GET /countries: list all countries
HEAD /countries: show the overview information of country listing
POST /countries: create a new country
GET /countries/AU: return the details of the country AU
HEAD /countries/AU: show the overview information of country AU
PATCH /countries/AU: update the country AU
PUT /countries/AU: update the country AU
DELETE /countries/AU: delete the country AU
OPTIONS /countries: show the supported verbs regarding endpoint /countries
OPTIONS /countries/AU: show the supported verbs regarding endpoint /countries/AU.

If you are extending app\controllers, I know you can use

 Countries::find()->limit(20); 


but I am working with \yii\rest\ActiveController
Re: Yii2 Rest API Question by Nobody: 1:19pm On May 13, 2016
tosinhtml:


ok, so Yii2 totally eliminates the CDbCriteria ??

Yep much more easier and less syntax look

Query
yii\db\ActiveQuery
Re: Yii2 Rest API Question by foldl: 1:57am On May 15, 2016
When you extend \yii\rest\ActiveController you do not need to render your result but return it directly.

In your example change '$this->render($model);' to 'return $model;'. Also note that since you are using findOne it will return the first record in that table only.

In general, you should return any value or object instead of rendering when using rest controllers. You don't need to serialize the data into json as that is done implicitly by the \yii\rest\ActiveController class. When in doubt read the source code!

I built a production system with yii2 when it was still in beta. I even found and fixed a bug and sent a pull request which was merged. But I haven't written any code in months so I might be a bit rusty. grin
Re: Yii2 Rest API Question by CCsurplus007(m): 12:44pm On May 18, 2016
nollyj:
Thanks all.

In Yii 2, when you controller class extends \yii\rest\ActiveController for Restful Api not app\controllers, the following methods are created like default


public function actionIndex(){

}

public function actionView(){

}

public function actionCreate(){

}

public function actionUpdate(){

}

public function actionDelete(){

}

public function actionOptions(){

}


This will map to the corresponding API end-points

GET /countries: list all countries
HEAD /countries: show the overview information of country listing
POST /countries: create a new country
GET /countries/AU: return the details of the country AU
HEAD /countries/AU: show the overview information of country AU
PATCH /countries/AU: update the country AU
PUT /countries/AU: update the country AU
DELETE /countries/AU: delete the country AU
OPTIONS /countries: show the supported verbs regarding endpoint /countries
OPTIONS /countries/AU: show the supported verbs regarding endpoint /countries/AU.

If you are extending app\controllers, I know you can use

 Countries::find()->limit(20); 


but I am working with \yii\rest\ActiveController
Pleasant greetings to you. I sent you a PM , can you kindly respond. I recently got a SI scholarship to study at Lulea and would need few clarifications from you.. . thanks.
Re: Yii2 Rest API Question by nollyj: 3:18pm On May 18, 2016
CCsurplus007:

Pleasant greetings to you. I sent you a PM , can you kindly respond. I recently got a SI scholarship to study at Lulea and would need few clarifications from you.. . thanks.

Lulea is Northern Sweden I live in Southern Sweden. I have not been to the Northern Part of Southern. Know also the weather in the North is very harshe during winter than in the south.

LuleƄ University of Technology is a nice school and I hope you will enjoy your time there

You can ask me question through email if the question is personal.

(1) (Reply)

Please Help Me Become A Programmer If You Can / How Can I Track Fone Number Location In Nigeria / Some One Should Help Me With This Windowing Issue

(Go Up)

Sections: politics (1) business autos (1) jobs (1) career education (1) romance computers phones travel sports fashion health
religion celebs tv-movies music-radio literature webmasters programming techmarket

Links: (1) (2) (3) (4) (5) (6) (7) (8) (9) (10)

Nairaland - Copyright © 2005 - 2024 Oluwaseun Osewa. All rights reserved. See How To Advertise. 30
Disclaimer: Every Nairaland member is solely responsible for anything that he/she posts or uploads on Nairaland.