Try the beta version of the new SilverStripe documentation

For the next 2 weeks you can use the new documentation website and give us your feedback.


This document contains information for an outdated version (2.4) and may not be maintained any more.

If some of your projects still use this version, consider upgrading as soon as possible.

Tutorial 4 - Site Search


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, 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.

What are we working towards?

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.

Creating the search form

The Search Form functionality has been altered over time. Please use the section which applies to your SilverStripe version.

2.4 and newer

SilverStripe 2.4 does not come bundled with the search engine enabled. To enable the search engine you need to include the following code in your mysite/_config.php file


After including that in your _config.php you will need to rebuild the database by visiting in your web browser. This will add the fulltext search columns.

The actual search form code is already provided in FulltextSearchable so when you add the enable line above to your _config you can add your form as $SearchForm.


SilverStripe 2.3 came bundled with the code as well as a MySQL Search engine. If you are using the blackcandy theme you should have everything you need already to have a search. If you are using the tutorial theme then you can simply skip down to 'Adding the search form' as the PHP code is already provided in Page.php. If it is not then you can follow the instructions below as well.


If you are using SilverStripe 2.2 or earlier then you need to define your own code. The first step in implementing search on your site is to create a form for the user to type their query. Create a function named SearchForm on the Page_Controller class (/mysite/code/Page.php).

class Page_Controller extends ContentController {
    function SearchForm() {
        $searchText = isset($this->Query) ? $this->Query : 'Search';
        $fields = new FieldSet(
            new TextField("Search", "", $searchText)
        $actions = new FieldSet(
            new FormAction('results', 'Go')
        return new SearchForm($this, "SearchForm", $fields, $actions);

Custom CSS code

Add the following css code to the themes/tutorial/css/layout.css file. This will style the header form and search results page.

#Header form {
    margin:25px 25px 0px 25px;
    #Header form * {
        display:inline !important;
    #Header form div {
    #Header form input.text {
        border:1px solid #aaa;
    #Header form input.action {
.searchResults h2 {
.searchResults p.searchQuery {
.searchResults ul#SearchResults li {
    ul#SearchResults p {
    ul#SearchResults a.searchResultHeader {
        margin:20px 0 8px 0;
        background:url(../images/treeicons/search-file.gif) no-repeat left center;
        ul#SearchResults a {
        ul#SearchResults a:hover {
        border-bottom:1px dotted #0083C8;

Adding the search form

We then just need to add the search form to the template. Add $SearchForm to the 'Header' div in themes/tutorial/templates/


<div id="Header">

Showing the results

Next we need to create the results function.


class Page_Controller extends ContentController {
    function results($data, $form){
        $data = array(
            'Results' => $form->getResults(),
            'Query' => $form->getSearchQuery(),
            'Title' => 'Search Results'
        $this->Query = $form->getSearchQuery();
        return $this->customise($data)->renderWith(array('Page_results', 'Page'));

First we populate 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.

When we call a function by its url (eg http://localhost/home/results), SilverStripe will look for a template with the name As we are implementing the results function on the Page page type, we create our results page template as 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 rather than 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:


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, falling back to if there is no

Creating the template

Lastly we need to create the template for the search page. This template uses all the same techniques used in previous tutorials. It also uses a number of pagination variables, which are provided by the DataObjectSet class.


<div id="Content" class="searchResults">
    <% if Query %>
        <p class="searchQuery"><strong>You searched for &quot;{$Query}&quot;</strong></p>
    <% end_if %>
    <% if Results %>
    <ul id="SearchResults">
        <% control Results %>
            <a class="searchResultHeader" href="$Link">
                <% if MenuTitle %>
                <% else %>
                <% end_if %>
            <a class="readMoreLink" href="$Link" title="Read more about &quot;{$Title}&quot;">Read more about &quot;{$Title}&quot;...</a>
        <% end_control %>
    <% else %>
    <p>Sorry, your search query did not return any results.</p>
    <% end_if %>
    <% if Results.MoreThanOnePage %>
    <div id="PageNumbers">
        <% if Results.NotLastPage %>
        <a class="next" href="$Results.NextLink" title="View the next page">Next</a>
        <% end_if %>
        <% if Results.NotFirstPage %>
        <a class="prev" href="$Results.PrevLink" title="View the previous page">Prev</a>
        <% end_if %>
            <% control Results.Pages %>
                <% if CurrentBool %>
                <% else %>
                <a href="$Link" title="View page number $PageNum">$PageNum</a>
                <% end_if %>
            <% end_control %>
        <p>Page $Results.CurrentPage of $Results.TotalPages</p>
    <% end_if %>

Then finally add ?flush=1 to the URL and you should see the new template.


This tutorial has demonstrated how easy it is to have full text searching on your site. To add search to a SilverStripe site, only a search form and a results page need to be created.

Next Tutorial >>


Comment policy: Please use comments for tips and corrections about the described functionality.
Comments are moderated, we reserve the right to remove comments that are inappropriate or are no longer relevant. Use the Silverstripe Forum to ask questions.

blog comments powered by Disqus