4

I can't seem to find this anywhere. Thanks.

Update:

'api/entry/search.json' => function() {
    return [
        'elementType' => 'Entry',
        'criteria' => [
            'search' => (craft()->request->getParam('search')) ? craft()->request->getParam('search') : '' 
        ],
        'first' => true,
        'transformer' => function(EntryModel $entry) {
            return [
                'title' => $entry->title,
                'url' => $entry->url,
            ];
        },
    ];
},

Using the url: api/entry/search.json?search=title:test did not work.


This code works, but the title needs to be exact:

'api/entry/search.json' => function() {
    return [
        'elementType' => 'Entry',
        'criteria' => [
            'title' => craft()->request->getParam('title')
        ],
        'first' => true,
        'transformer' => function(EntryModel $entry) {
            return [
                'title' => $entry->title,
                'url' => $entry->url,
            ];
        },
    ];
},

Using this url: api/entry/search.json?title=Test%20Article, works, but not api/entry/search.json?title=Test

Erik
  • 306
  • 2
  • 9

1 Answers1

5

Following is an example of using the search feature in a GET request with parameter 'search'.

return [
    'elementType' => 'Entry',
    'criteria' => ['search' => (craft()->request->getParam('search')) ? 'title:'.craft()->request->getParam('search') : '' ],
    'paginate' => true,
    'elementsPerPage' => 10,
    'pageParam' => 'pg',
    'transformer' => new ListTransformer(),
];`

I assume you can use this instead of the custom transformer class that is being used in the above example:

'transformer' => function(EntryModel $entry) {
    return [
        'title' => $entry->title,
        'url' => $entry->url,
        'summary' => $entry->summary,
        'body' => $entry->body,
    ];
},
Brad Bell
  • 67,440
  • 6
  • 73
  • 143
Mark Watson
  • 150
  • 6