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.

General Questions /

General questions about getting started with SilverStripe that don't fit in any of the categories above.

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

Search function on top


Go to End


9 Posts   2512 Views

Avatar
alienn

Community Member, 15 Posts

18 October 2011 at 12:21pm

Hey there
I want to create search function on url mysite.com/search - no search form on top of the page, just single subpage with search form and results
i've used the tutorial but it's not working (completly, got 404) - how to force the controller to display content from "search" method in the Page class insteed of 404 ? ?

Avatar
sheadawson

Community Member, 49 Posts

18 October 2011 at 7:56pm

Hey, maybe you could post your code?

Avatar
alienn

Community Member, 15 Posts

18 October 2011 at 9:45pm

it's just copypasted code from tutorial here: http://doc.silverstripe.org/sapphire/en/tutorials/4-site-search for "2.4 and newer"
with a differences:
- no $SearchForm on top of the page, just link to /search
- renamed function results to function search and Page_results to Page_search

i'll post exact code when i'll be back on my work computer, but like ive said its just copypasted

what possibly i've missed ?

Avatar
alienn

Community Member, 15 Posts

24 October 2011 at 10:46am

<?php
class SearchPage extends SiteTree {

}

class SearchPage_Controller extends Page_Controller {
	
	public function index() {
		return $this->render();
	}
	
	public 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('SearchPage_results','SearchPage'));
	}
}
?>

this is what i've done so far and still completly not working.
on index i just display template with $SearchForm added

code aboce generates form like:

<form  id="SearchForm_SearchForm" action="/SearchPage_Controller/SearchForm" method="get" enctype="application/x-www-form-urlencoded">

how to change $searchForm ? need to change action value, and the submit button?

Avatar
Webdoc

Community Member, 349 Posts

24 October 2011 at 11:04am

Edited: 24/10/2011 11:04am

what version on ss are u using if 2.4.5 add in your mysite/_config.php this line - FulltextSearchable::enable(); - this line enables the search form

Avatar
alienn

Community Member, 15 Posts

24 October 2011 at 11:08am

Edited: 24/10/2011 11:10am

im using 2.4.5.
this line is added, rebuilded etc. i've followed the tutorial, but it didn't helped as only the (poorly working) search form is displayed, nothing else

Avatar
Webdoc

Community Member, 349 Posts

24 October 2011 at 11:34am

Page_results.ss under yourtheme/layout/ must contains something like that:

<div class="typography">
<% if Results %>
<ul id="SearchResults">
<% control Results %>
<li>
<% if MenuTitle %>
<h3><a class="searchResultHeader" href="$Link">$MenuTitle</a></h3>
<% else %>
<h3><a class="searchResultHeader" href="$Link">$Title</a></h3>
<% end_if %>
<% if Content %>
$Content.FirstParagraph(html)
<% end_if %>
<a class="readMoreLink" href="$Link" title="Read more about &quot;{$Title}&quot;">Read more about &quot;{$Title}&quot;...</a>
</li>
<% end_control %>
</ul>
<% else %>
<p>
<%-- Example of a translatable string (see http://doc.silverstripe.org/i18n) --%>
<% _t("Page_results.ss.NORESULTS", "Sorry, your search query did not return any results.") %>
<%-- By the way, template comments marked like this will be excluded from the HTML output --%>
</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 %>
<span>
<% control Results.SummaryPagination(5) %>
<% if CurrentBool %>
$PageNum
<% else %>
<a href="$Link" title="View page number $PageNum">$PageNum</a>
<% end_if %>
<% end_control %>
</span>

</div>
<% end_if %>
</div>

Avatar
alienn

Community Member, 15 Posts

25 October 2011 at 9:58am

i've copied Page_results.ss from tutorial, but it's dont even accessed - i'm redirected to /SearchPage_Controller/SearchForm.
from the tutorial only input and submit button are working somehow (by working i meant that they are displayed, nothing more)

Go to Top