PHP | Working Example - Pageless Controller for displaying searchresults
<?php
/**
* _config.php
*
* //Enable search
* FulltextSearchable::enable();
* //Add extensions
* Object::add_extension('Page_Controller','SearchFormExtension');
* //Set urlsegment
* SearchPage_Controller::setURLSegment('searchresults');
* //Set director to SearchPage_Controller
* Director::addRules(10, array(
* SearchPage_Controller::getURLSegment().'//$Action/$ID/$OtherID' => 'SearchPage_Controller'
* ));
*
* theme/Layout/SearchPage.ss
*
* <% if SearchResults %>
* <div class="searchresults">
* <% control SearchResults %>
* <div>
* <h5>$Title</h5>
* <p>$Content.Summary</p>
* <a class="readMoreLink right" href="$Link" title="$Title">Read more</a>
* <div class="clear"></div>
* </div>
* <% end_control %>
* </div>
* <% else %>
* <p>No results found</p>
* <% end_if %>
*/
/**
* Add a SearchForm to Page_Controller
*/
class SearchFormExtension extends Extension{
public static $allowed_actions = array(
'SearchForm',
'results'
);
function SearchForm() {
$searchText = $this->owner->request->getVar('Search') ? $this->owner->request->getVar('Search') : 'Search';
$fields = new FieldSet(
new TextField("Search", "", $searchText)
);
$actions = new FieldSet(
new FormAction('results', 'Search!')
);
return new SearchForm(Controller::curr(), "SearchForm", $fields, $actions);
}
function results($data = NULL, $form = NULL){
if($data && $form){
Director::redirect(Director::baseURL().SearchPage_Controller::getURLSegment()."/?Search=".$form->getSearchQuery());
}
}
}
/**
* Pageless controller to display the searchresults
*/
class SearchPage_Controller extends Page_Controller{
public static $url_segment = 'searchresults';
function init(){
parent::init();
}
function getURLSegment(){
return self::$url_segment;
}
function setURLSegment($string = 'searchresults'){
self::$url_segment = $string;
}
function SearchResults(){
if($this->request->getVar('Search')){
$form = SearchFormExtension::SearchForm();
return $form->getResults();
}
}
function Link(){
return $this->getURLSegment();
}
}
Not extensively tested Extension and Controller to display a SearchForm on Pages and the results on a Pageless Controller. |
|
| Tags : Controller SearchForm Page | Reference : http://silverstripe.org/form-questions/show/276960 |
04/08/2011 6:37am
PHP | Comment - Bunheng
Hi
is it possible to return the Title for result page and the number of results I am trying it show the results but there is no page title.
Bunheng