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.

Form Questions /

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

How to display Search Results on the same URL


Go to End


18 Posts   11738 Views

Avatar
Martijn

Community Member, 271 Posts

6 September 2010 at 11:02pm


Not extensively tested, but this creates a SearchForm on Page with an Extension and displays the searchresults with a Controller without a Page class.

http://www.sspaste.com/paste/show/4c84cb3f4d2e1

Avatar
losqualo

Community Member, 17 Posts

7 September 2010 at 12:45am

Apologies if I'm being particularly dense here... does that all go into _config.php as seems to be implied?

If so, why? Seems to run counter to everything I've read thus far if that's the case.

And why is the fulltext search engine enabling commented out?.. that also seems counter-intuitive.

If not, then how does it break down in terms of files and where they go... I'd be assuming as php files in mysite/code... that is if it isn't just in the _config.php file.

That said, once I understand the placement/breakdown I can see some things in here that help in other areas of understanding, so thanks for that too!

Cheers

Rob

Avatar
Martijn

Community Member, 271 Posts

7 September 2010 at 4:17am

Edited: 07/09/2010 4:25am

No, sorry if I was not clear.

This goes in you _config.php file :

//Enable search
FulltextSearchable::enable();
//Add extensions
Object::add_extension('Page_Controller','SearchFormExtension');
//Set director to SearchPage_Controller
Director::addRules(10, array(
	SearchPage_Controller::getURLSegment().'//$Action/$ID/$OtherID' => 'SearchPage_Controller'
));

This goes in themes/yourtheme/Layout/SearchPage.ss:

<% if SearchResults %>
	<div class="searchresults">
	<% control SearchResults %>
		<div>
			<h5>$Title</h5>
			<p>$Content.Summary</p>
			<a class="readMoreLink right" href="$Link" title="$Title">Read more</a>
			<div class="clear"></div>	
		</div>
	<% end_control %>
	</div>
<% else %>
<p>No results found</p>
<% end_if %>

And this goes in mysite/code/SearchFormExtension.php:

<?php
/**
 * Add a SearchForm to Page_Controller
 */
class SearchFormExtension extends Extension{
	
	public static $allowed_actions = array(
		'SearchForm',
		'results'
	);
	
	function SearchForm() {
		
		$searchText = $this->owner->request->getVar('Search') ? $this->owner->request->getVar('Search') : 'Search';
		
		$fields = new FieldSet(
			new TextField("Search", "", $searchText)
		);
		$actions = new FieldSet(
			new FormAction('results', 'Search!')
		);
		
		return new SearchForm(Controller::curr(), "SearchForm", $fields, $actions);
	} 	
	
	function results($data = NULL, $form = NULL){
		if($data && $form){
			Director::redirect(Director::baseURL().SearchPage_Controller::getURLSegment()."/?Search=".$form->getSearchQuery());
		}
	}
}

/**
 * Pageless controller to display the searchresults
 */
 
class SearchPage_Controller extends Page_Controller{
	
	public static $url_segment = 'searchresults';
	
	function init(){
		parent::init();
	}
	
	function getURLSegment(){
		return self::$url_segment;	
	}
	
	function setURLSegment($string = 'searchresults'){
		self::$url_segment = $string;	
	}
	
	function SearchResults(){
		if($this->request->getVar('Search')){
			$form = SearchFormExtension::SearchForm();
			return $form->getResults();
		}
   }
   
   function Link(){
		return $this->getURLSegment();  
   }
}

Edit: I attached a zipfile with the needed files in a seperate modulefolder.
This should work as well.

Attached Files
Avatar
bunheng

Community Member, 78 Posts

4 August 2011 at 4:13pm

Hi,

Is it possible to return the the following:

1. Page title for SearchPage.
2. Total results match.
3. And the search query.

Thanks
Bunheng

Avatar
martimiz

Forum Moderator, 1391 Posts

5 August 2011 at 2:53am

Edited: 05/08/2011 2:54am

@ Yitter: I still think you're on the right track using $form->setFormAction. I can confirm the missing &action_results=Search in IE, I recall that IE has trouble returning the name of the input 'submit'. But that doesn't really matter, as long as you're not using it...

I'll add this example to all the different solutions offered in this thread: a simple search in a basic page. It's almost like the basic SilverStripe search, but for me it works on IE6789... I stripped it bare for the sake of a clean example:

- first I created a page called 'searchresults' in the SiteTree. Then in my Page_Controller:

protected $Query;

function SearchForm() {
	$searchText = isset($this->Query) ? $this->Query : 'Search...';
	$fields = new FieldSet(
      		new TextField("Search", "", $searchText)
  	);
	$actions = new FieldSet(
      		new FormAction('results', 'Search')
  	);
  	$form = new SearchForm($this, "SearchForm", $fields, $actions);
	$form->setFormAction(Director::baseURL().'searchresults/SearchForm');
	return $form;
}


function results($data, $form){
	$Query = (isset($data['Search']))? $data['Search'] : '' ;
	$sqlQuery = Convert::raw2sql($Query);
	$Title   = "Search results for '" . substr($Query, 0, 20) . "'  ";

	$Results = DataObject::get( ... );
	$searchdata = array(
		'ProductList' => $Results,
		'MenuTitle' => $Title
  	);
	$this->Query = $Query;
	return $this->customise($searchdata)->renderWith(array('PageResults', 'Page'));
}

@bunheng: sure you can, add all the info you want to the $searchdata array (like I did with the MenuTitle), and make sure your template uses them..

Avatar
bunheng

Community Member, 78 Posts

5 August 2011 at 1:16pm

Hi martimiz,

Sorry to disturb you again, I would like to know where I can create the page searchresults, is it a new page class in mysite/code? or create it via CMS Admin.

Thanks
Bunheng

Avatar
martimiz

Forum Moderator, 1391 Posts

10 August 2011 at 12:22am

Edited: 10/08/2011 12:22am

Hi again

I'm sorry, I missed your question untill right now :-(

Anyway: I meant create a new page in the CMS SiteTree. Title: searchresults

Avatar
NickJacobs

Community Member, 148 Posts

22 August 2011 at 3:13am

Hi @Martijn
I setup your SearchFormExtension, and it works great.....but once I log out of SS I cannot log back in. I get an unknown 500 server error..

if I comment out the 'Object::add_extension('Page_Controller','SearchFormExtension');' I can get back in, bu then the search obviously doesn't work..

any ideas why this would be happening?