2014-11-11 00:34:23 +01:00
title: Site Search
2014-09-20 04:10:49 +02:00
summary: Enable website search. How to handle paged result sets and the SearchForm class.
2011-02-07 07:48:44 +01:00
# Tutorial 4 - Site Search
## Overview
2014-11-11 00:34:23 +01:00
This is a short tutorial demonstrating how to add search functionality to a SilverStripe site. It is recommended that you have completed the earlier tutorials ([Building a basic site](/tutorials/building_a_basic_site), [Extending a basic site ](/tutorials/extending_a_basic_site ), [Forms ](/tutorials/forms )), especially the tutorial on forms, before attempting this tutorial. While this tutorial will add search functionality to the site built in the previous tutorials, it should be straight forward to follow this tutorial on any site of your own.
2011-02-07 07:48:44 +01:00
## What are we working towards?
2012-07-02 04:28:42 +02:00
We are going to add a search box on the top of the page. When a user types something in the box, they are taken to a results page.
2011-02-07 07:48:44 +01:00
2014-12-18 00:11:57 +01:00
![](../_images/tutorial4_search.jpg)
2011-02-07 07:48:44 +01:00
## Creating the search form
2011-12-03 12:00:56 +01:00
To enable the search engine you need to include the following code in your `mysite/_config.php` file.
This will enable fulltext search on page content as well as names of all files in the `/assets` folder.
2011-02-07 07:48:44 +01:00
:::php
FulltextSearchable::enable();
2012-10-23 02:37:01 +02:00
After including that in your `_config.php` you will need to rebuild the database by visiting [http://localhost/your_site_name/dev/build ](http://localhost/your_site_name/dev/build ) in your web browser (replace localhost/your_site_name with a domain if applicable). This will add fulltext search columns.
2011-02-07 07:48:44 +01:00
2012-07-20 01:21:52 +02:00
The actual search form code is already provided in FulltextSearchable so when you add the enable line above to your `_config.php` you can add your form as `$SearchForm` .
2011-02-07 07:48:44 +01:00
2012-06-08 00:37:16 +02:00
In the simple theme, the SearchForm is already added to the header. We will go through the code and explain it.
2011-03-08 22:05:51 +01:00
2011-02-07 07:48:44 +01:00
## Adding the search form
2012-07-20 01:21:52 +02:00
To add the search form, we can add `$SearchForm` anywhere in our templates. In the simple theme, this is in *themes/simple/templates/Includes/Header.ss*
2011-02-07 07:48:44 +01:00
2012-07-20 01:21:52 +02:00
**themes/simple/templates/Includes/Header.ss**
2011-02-07 07:48:44 +01:00
:::ss
2012-06-08 00:37:16 +02:00
...
2013-04-26 11:48:59 +02:00
< % if $SearchForm %>
2012-06-08 00:37:16 +02:00
< span class = "search-dropdown-icon" > L< / span >
< div class = "search-bar" >
$SearchForm
< / div >
< % end_if %>
< % include Navigation %>
2011-02-07 07:48:44 +01:00
2012-07-20 01:21:52 +02:00
This displays as:
2011-02-07 07:48:44 +01:00
2014-12-18 00:11:57 +01:00
![](../_images/tutorial4_searchbox.jpg)
2011-02-07 07:48:44 +01:00
## Showing the results
2012-06-08 00:37:16 +02:00
The results function is already included in the `ContentControllerSearchExtension` which
is applied via `FulltextSearchable::enable()`
2011-02-07 07:48:44 +01:00
2012-07-20 01:21:52 +02:00
**cms/code/search/ContentControllerSearchExtension.php**
2011-02-07 07:48:44 +01:00
:::php
2012-06-08 00:37:16 +02:00
class ContentControllerSearchExtension extends Extension {
2011-02-07 07:48:44 +01:00
...
2012-06-08 00:37:16 +02:00
function results($data, $form, $request) {
2011-02-07 07:48:44 +01:00
$data = array(
'Results' => $form->getResults(),
'Query' => $form->getSearchQuery(),
2012-06-08 00:37:16 +02:00
'Title' => _t('SearchForm.SearchResults', 'Search Results')
2011-02-07 07:48:44 +01:00
);
2012-06-08 00:37:16 +02:00
return $this->owner->customise($data)->renderWith(array('Page_results', 'Page'));
2011-02-07 07:48:44 +01:00
}
}
2012-07-20 01:21:52 +02:00
The code populates an array with the data we wish to pass to the template - the search results, query and title of the page. The final line is a little more complicated.
2011-02-07 07:48:44 +01:00
2012-07-20 01:21:52 +02:00
When we call a function by its url (eg http://localhost/home/results), SilverStripe will look for a template with the name `PageType_function.ss` . As we are implementing the *results* function on the *Page* page type, we create our
2011-02-07 07:48:44 +01:00
results page template as *Page_results.ss* . Unfortunately this doesn't work when we are using page types that are
children of the *Page* page type. For example, if someone used the search on the homepage, it would be rendered with
*Homepage.ss* rather than *Page_results.ss* . SilverStripe always looks for the template from the most specific page type
first, so in this case it would use the first template it finds in this list:
* HomePage_results.ss
* HomePage.ss
* Page_results.ss
* Page.ss
We can override this list by using the *renderWith* function. The *renderWith* function takes an array of the names of
the templates you wish to render the page with. Here we first add the data to the page by using the 'customise'
function, and then attempt to render it with *Page_results.ss* , falling back to *Page.ss* if there is no
*Page_results.ss*.
## Creating the template
2012-06-08 00:37:16 +02:00
Lastly we need the template for the search page. This template uses all the same techniques used in previous
2016-02-17 10:26:06 +01:00
tutorials. It also uses a number of pagination variables, which are provided by the [api:PaginatedList]
2011-02-07 07:48:44 +01:00
class.
2012-06-08 00:37:16 +02:00
*themes/simple/templates/Layout/Page_results.ss*
2011-02-07 07:48:44 +01:00
:::ss
< div id = "Content" class = "searchResults" >
2012-06-08 00:37:16 +02:00
< h1 > $Title< / h1 >
2013-04-26 11:48:59 +02:00
< % if $Query %>
2012-06-08 00:37:16 +02:00
< p class = "searchQuery" > < strong > You searched for " {$Query}" < / strong > < / p >
< % end_if %>
2013-04-26 11:48:59 +02:00
< % if $Results %>
2012-06-08 00:37:16 +02:00
< ul id = "SearchResults" >
2013-04-26 11:48:59 +02:00
< % loop $Results %>
2012-06-08 00:37:16 +02:00
< li >
< a class = "searchResultHeader" href = "$Link" >
2013-04-26 11:48:59 +02:00
< % if $MenuTitle %>
2012-06-08 00:37:16 +02:00
$MenuTitle
< % else %>
$Title
< % end_if %>
< / a >
< p > $Content.LimitWordCountXML< / p >
< a class = "readMoreLink" href = "$Link"
title="Read more about " {$Title}" "
>Read more about " {$Title}" ...< / a >
< / li >
2012-06-26 17:32:46 +02:00
< % end_loop %>
2012-06-08 00:37:16 +02:00
< / ul >
< % else %>
< p > Sorry, your search query did not return any results.< / p >
< % end_if %>
2013-04-26 11:48:59 +02:00
< % if $Results.MoreThanOnePage %>
2012-06-08 00:37:16 +02:00
< div id = "PageNumbers" >
2013-04-26 11:48:59 +02:00
< % if $Results.NotLastPage %>
2012-06-08 00:37:16 +02:00
< a class = "next" href = "$Results.NextLink" title = "View the next page" > Next< / a >
< % end_if %>
2013-04-26 11:48:59 +02:00
< % if $Results.NotFirstPage %>
2012-06-08 00:37:16 +02:00
< a class = "prev" href = "$Results.PrevLink" title = "View the previous page" > Prev< / a >
< % end_if %>
< span >
2013-04-26 11:48:59 +02:00
< % loop $Results.Pages %>
< % if $CurrentBool %>
2012-06-08 00:37:16 +02:00
$PageNum
< % else %>
< a href = "$Link" title = "View page number $PageNum" > $PageNum< / a >
< % end_if %>
2012-06-26 17:32:46 +02:00
< % end_loop %>
2012-06-08 00:37:16 +02:00
< / span >
< p > Page $Results.CurrentPage of $Results.TotalPages< / p >
< / div >
< % end_if %>
2011-02-07 07:48:44 +01:00
< / div >
2012-10-23 02:37:01 +02:00
Then finally add ?flush=1 to the URL and you should see the new template.
2011-02-07 07:48:44 +01:00
2014-12-18 00:11:57 +01:00
![](../_images/tutorial4_search.jpg)
2011-02-07 07:48:44 +01:00
## Summary
2012-07-20 01:21:52 +02:00
This tutorial has demonstrated how easy it is to have full text searching on your site. To add search to a SilverStripe site add a search form, a results page, and you're done!
2011-02-07 07:48:44 +01:00
2014-11-11 00:34:23 +01:00
[Next Tutorial >> ](/tutorials/dataobject_relationship_management )