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
2017-08-03 00:46:55 +02:00
```php
2017-10-27 04:38:27 +02:00
FulltextSearchable::enable();
```
2011-02-07 07:48:44 +01:00
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
2017-08-03 00:46:55 +02:00
```ss
2017-10-27 04:38:27 +02:00
< % if $SearchForm %>
< span class = "search-dropdown-icon" > L< / span >
< div class = "search-bar" >
$SearchForm
< / div >
< % end_if %>
< % include Navigation %>
2017-08-03 00:46:55 +02:00
```
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
2017-08-03 00:46:55 +02:00
```php
2017-10-27 04:38:27 +02:00
use SilverStripe\Core\Extension;
2017-08-07 05:11:17 +02:00
2017-10-27 04:38:27 +02:00
class ContentControllerSearchExtension extends Extension
{
public function results($data, $form, $request)
2017-08-07 05:11:17 +02:00
{
2017-10-27 04:38:27 +02:00
$data = [
'Results' => $form->getResults(),
'Query' => $form->getSearchQuery(),
'Title' => _t('SearchForm.SearchResults', 'Search Results')
];
return $this->owner->customise($data)->renderWith(['Page_results', 'Page']);
2017-08-07 05:11:17 +02:00
}
2017-10-27 04:38:27 +02:00
}
2017-08-03 00:46:55 +02:00
```
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
2017-07-03 03:22:12 +02:00
tutorials. It also uses a number of pagination variables, which are provided by the [PaginatedList ](api:SilverStripe\ORM\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
2017-08-03 00:46:55 +02:00
```ss
2017-10-27 04:38:27 +02:00
< div id = "Content" class = "searchResults" >
< h1 > $Title< / h1 >
< % if $Query %>
< p class = "searchQuery" > < strong > You searched for " {$Query}" < / strong > < / p >
< % end_if %>
2017-08-07 05:11:17 +02:00
2017-10-27 04:38:27 +02:00
< % if $Results %>
< ul id = "SearchResults" >
< % loop $Results %>
< li >
< a class = "searchResultHeader" href = "$Link" >
< % if $MenuTitle %>
$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 >
< % end_loop %>
< / ul >
< % else %>
< p > Sorry, your search query did not return any results.< / p >
< % end_if %>
2017-08-07 05:11:17 +02:00
2017-10-27 04:38:27 +02:00
< % if $Results.MoreThanOnePage %>
< div id = "PageNumbers" >
< % if $Results.NotLastPage %>
< a class = "next" href = "$Results.NextLink" title = "View the next page" > Next< / a >
2017-08-07 05:11:17 +02:00
< % end_if %>
2017-10-27 04:38:27 +02:00
< % if $Results.NotFirstPage %>
< a class = "prev" href = "$Results.PrevLink" title = "View the previous page" > Prev< / a >
2017-08-07 05:11:17 +02:00
< % end_if %>
2017-10-27 04:38:27 +02:00
< span >
< % loop $Results.Pages %>
< % if $CurrentBool %>
$PageNum
< % else %>
< a href = "$Link" title = "View page number $PageNum" > $PageNum< / a >
< % end_if %>
< % end_loop %>
< / span >
< p > Page $Results.CurrentPage of $Results.TotalPages< / p >
2017-08-07 05:11:17 +02:00
< / div >
2017-10-27 04:38:27 +02:00
< % end_if %>
< / div >
2017-08-03 00:46:55 +02:00
```
2011-02-07 07:48:44 +01:00
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 )