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

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,
    ];
},
7
  • How would I use this in the URL? Like this: search.json?search=title:test ?
    – Erik
    Jun 1 '16 at 14:06
  • URL format is correct I haven't delved too deeply into search syntax for parameters like title:test mine is search.json?search=test that is global search Jun 1 '16 at 14:16
  • The syntax for search=title:test is correct Jun 1 '16 at 14:18
  • Hmm, ok. Doesn't seem to fetch anything. I will tinker with it. Thanks.
    – Erik
    Jun 1 '16 at 14:18
  • Try this title:"*test*" Jun 1 '16 at 14:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.