As docs say: [[yii\\rest\\IndexAction|index]]: list resources page by page
response has view:
curl -i -H "Accept:application/json" "http://192.168.100.5/index.php/tweets"
HTTP/1.1 200 OK
Date: Wed, 30 Mar 2016 12:10:07 GMT
Server: Apache/2.4.7 (Ubuntu)
X-Powered-By: PHP/5.5.9-1ubuntu4.14
X-Pagination-Total-Count: 450
X-Pagination-Page-Count: 23
X-Pagination-Current-Page: 1
X-Pagination-Per-Page: 20
Link: <http://192.168.100.5/tweets?page=1>; rel=self, <http://192.168.100.5/tweets?page=2>; rel=next, <http://192.168.100.5/tweets?page=23>; rel=last
Content-Length: 4305
Content-Type: application/json; charset=UTF-8
[{"id":71,"text":"Juíza do RS Graziela Bünd.......
I have component which one return - some array (selection from two tables). If i customize indexAction.
public function actions()
{
$actions = parent::actions();
unset($actions['update']);
unset($actions['delete']);
unset($actions['view']);
unset($actions['index']);
return $actions;
}
public function actionIndex($count = 10)
{
/** @var TweetLastfinder $tweetLastFinder */
$tweetLastFinder = Yii::$app->get('tweetlastfinder');
return $tweetLastFinder->findLastTweets($count);
}
Response have correct content but has view:
curl -i -H "Accept:application/json" "http://192.168.100.5/index.php/tweets"
HTTP/1.1 200 OK
Date: Wed, 30 Mar 2016 12:15:36 GMT
Server: Apache/2.4.7 (Ubuntu)
X-Powered-By: PHP/5.5.9-1ubuntu4.14
Content-Length: 2282
Content-Type: application/json; charset=UTF-8
[{"id":605,"text":"Popular Mus......
In this case i cant use $serializer
, show _meta
etc
I want to use response from component and list resources page by page as it do default action. How it should be done properly?
To get full use of the built in yii\\rest\\Serializer and show _meta
or have your urls to look like:
/tweets?page=5&per-page=12&sort=name
your action should return a data provider object that implements the DataProviderInterface which can be any of those:
So it all depends on what kind of object $tweetLastFinder->findLastTweets()
is returning. If the findLastTweets
method is returning an ActiveQuery
object like:
public function findLastTweets($count)
{
...
return $Tweets::find();
}
Then just put it into an ActiveDataProvider
instance:
use yii\data\ActiveDataProvider;
public function actionIndex($count = 10)
{
/** @var TweetLastfinder $tweetLastFinder */
$tweetLastFinder = Yii::$app->get('tweetlastfinder');
$tweets = $tweetLastFinder->findLastTweets();
return new ActiveDataProvider([
'query' => $tweets,
]);
}
If it returns an array of data or something you can convert to an array then just put it into an ArrayDataProvider
instance. If it is a more complex object then you'll need to build a custom Data Provider inside which you can wrap it. See how to do that in related docs .
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.