Dedicated searchresults page

Dedicated searchpage

It's not at all difficult to create a simple(!) searchform on your site. In version 2.4 basically all you have to do is:

In _config.php:




How does that work?

The default SearchForm() function, that creates the form, and the results() function, that shows the results, are predefined in the ContentControllerSearchExtension class.

On calling FullTextSearchable::enable() in your _config, this decorator is automatically added to the ContentController class, from which all page controllers inherit. That, together with the template, is all you need to get things going.

Using a dedicated 'searchresults' page

Unfortunately your search results will always be displayed using the URL of the page you've started your search from, and the pagetype involved might not have the proper build for it. Better use a dedicated search results page!

So create a new page with URLSegment searchresults, publish it (and hide it from the menu). If you want you can even create a dedicated pagetype for it. Now you need to tell the SearchForm to use this searchresults page.For that you need to overload SearchForm()

In your Page_Controller:

function SearchForm() {
	$form = parent::SearchForm();


	return $form;

The form action now uses the URLSegment searchresults as well as the SearchForm action, so submitting the form will lead you straight to the searchresults page. Now, if you incorporate this functionality in your Page_Controller, wouldn't it be great to have that page available on install? That's really easy: read more about creating default records on install here...

Create the 'searchresults' page on install

In this example, I'm using a default Page type, but you can use any pagetype you want - as long as it's available.

In the Page class:

function requireDefaultRecords() {

	if($this->class == 'Page') {

		// create an URLSegment for the page, based on the current language
		$searchpageURL = _t('Page.SEARCHPAGEURL', 'searchresults');

		if(!SiteTree::get_by_link($searchpageURL)) {

			$searchresults = new Page();
			$searchresults->Title = _t('Page.SEARCHRESULTSTITLE', 'SearchResults');
			$searchresults->URLSegment = $searchpageURL;
			$searchresults->Status = 'Published';
			$searchresults->ShowInMenus = false;
			$searchresults->ShowInSearch = false;
			$searchresults->Sort = 5;
			$searchresults->publish('Stage', 'Live');
			DB::alteration_message('Searchpage created', 'created');

Note that I've used a _t( ... ) translation to create the URLSegment - leave it out if you don't want it, but read below first!


In the example above I've created a translation for the PageURL, so it can be something corresponding to the websites language. If you want this, you need to make sure that the translation is available in the languagefile of your language. So for instance on a Dutch site this could be:


$lang['nl_NL']['Page']['SEARCHPAGEURL'] = 'zoekresultaten';
$lang['nl_NL']['Page']['SEARCHRESULTSTITLE'] = 'Zoekresultaten';

To make this work, you'll have to use the same translation in your Page_Controllers SearchForm() function.

In your Page_Controller:

function SearchForm() {
	$form = parent::SearchForm();

	$searchFormName = _t('Page.SEARCHPAGEURL','searchresults');


	return $form;

Translatable glitch?

There is one: if you create a translation of your site, your translated Searchresults page will not automatically get the translated URLSegment, so you'll have to do it by hand. But after that the search functionality will automatically call the right searchresults page for the current language, giving you all kinds of extra possibilities!


Think of a way for a searchresults page to automatically adopt the right URLSegment. Maybe even think of a way to use requireDefaultRecords to automatically create a set of default pages for a translated website, like for instance the 404, and the 500 errorpages - and the searchresults page?


Het versturen van reacties is uitgeschakeld.

RSS feed voor reacties op deze pagina