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

Calling an action on init()


Go to End


3 Posts   4229 Views

Avatar
CodeGuerrilla

Community Member, 105 Posts

3 February 2010 at 6:56pm

Edited: 03/02/2010 6:57pm

I have a product site that basically uses search filters to display the products all good basically have it working you select some filters and press go and it will return the products great.

My question is I would like to call the action 'search' on the controller without posting the form so by default it will return all of the products until the user selects some filters and presses go.

class ProductPage_Controller extends Page_Controller {
    ...
    public function init()
    {
        parent::init();
        if(!isset($_POST)) {
            return $this->search();
        }
    }

    public function search()
    {
         // do stuff return all results or results if $_POST filters set
         return $this->customise($data)->renderWith(array('ProductPage', 'Page'));
    }
}

Can this be done or is there an easier way?

Avatar
Hamish

Community Member, 712 Posts

3 February 2010 at 7:15pm

index() is the default method - ie, when no other action is request, index is called.

So, instead of using init() (which is called on every action and is not supposed to return a response), move your method to index() and it should work as intended.

Avatar
CodeGuerrilla

Community Member, 105 Posts

3 February 2010 at 7:21pm

Hey thanks Hamish works like a charm!

Kind of funny I was just reading Controllers that mentions that but it did not occur to me to actually try it :)