Skip to main content

This site requires you to update your browser. Your browsing experience maybe affected by not having the most up to date version.

We've moved the forum!

Please use forum.silverstripe.org for any new questions (announcement).
The forum archive will stick around, but will be read only.

You can also use our Slack channel or StackOverflow to ask for help.
Check out our community overview for more options to contribute.

Customising the CMS /

Moderators: martimiz, Sean, Ed, biapar, Willr, Ingo, swaiba

Custom Site Search Website


Go to End


4 Posts   1191 Views

Avatar
sherlack

Community Member, 10 Posts

8 July 2015 at 5:51am

Hi,

I'm working on an silverstripe / angularjs application. I would like to know if its possible to create a controller that would handle site search results. I would like to make a webservice that returns the silverstripe search result as json format. Is this possible to do with the ss api?

Thanks!

Avatar
Pyromanik

Community Member, 419 Posts

8 July 2015 at 10:29am

Yes.
Try something like:

class SiteSearch extends Controller {
	public function index() {
		return Form::create(
			$this, __FUNCTION__,
			FieldList::create(TextField::create('Search')),
			FieldList::create(FormAction::create('doSearch', 'Search'))
		)
		->setFormMethod('GET')
		->disableSecurityToken();
	}
	public function doSearch($data) {
		return json_encode(Things::get()->filterAny([
			'PropertyOne' => $data['Search'],
			'PropertyTwo' => $data['Search'],
			'PropertyThree' => $data['Search']
		])->toArray());
	}
}

Avatar
sherlack

Community Member, 10 Posts

9 July 2015 at 3:13am

Hi,

thanks for your response!

Based on your example, I created an endpoint '/api/search' and this is the get handler I have:

protected function getHandler() {	
		header('Content-Type: application/json');
		
		$query = $_GET['q'];
		$query = 'Contact Us';

		$query_result = SiteTree::get()->filterAny([
			'Title' => $query,
			'Content' => $query
		])->toArray();

		$result = array(
			"result" => $query_result
		);

		// output data in json format.
		echo json_encode($query_result);

		die;
	}

Problem I have it returns one record in this case but doesnt return all object fields. Output Example:

[
{
"destroyed": false,
"class": "Page"
}
]

I'm missing something? Also is there a way to paginate this result?

Thanks!

Avatar
Pyromanik

Community Member, 419 Posts

10 July 2015 at 1:36am

Edited: 10/07/2015 1:43am

Generally using a framework just to circumvent it is a bad idea.
You'll make more work for yourself, and not receive any of it's benefits.

http://api.silverstripe.org/3.1/class-SS_HTTPRequest.html

$query = $this->request->getVar('q');

http://api.silverstripe.org/3.1/class-SS_HTTPResponse.html
$this->response->addHeader('Content-Type', 'application/json');

As to the filterAny, try adding some filter modifiers.
$result = SiteTree::get()->filterAny([
'Title:PartialMatch' => $query,
'Content:PartialMatch' => $query
])->toArray()

And as to why it's probably not working, it seems like you've not found any results.
return json_encode(['result'=>$result->exists()?$result:null]);

SS returns a 'null object' pattern to facilitate chaining like what jQuery does. So test that you have results to ensure your result set makes sense.