Jump to:

23489 Posts in 18996 Topics by 2878 members

General Questions

SilverStripe Forums » General Questions » Calling an action on init()

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

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

Page: 1
Go to End
Author Topic: 2051 Views
  • CodeGuerrilla
    Avatar
    Community Member
    105 Posts

    Calling an action on init() Link to this post

    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?

  • Hamish
    Avatar
    Community Member
    712 Posts

    Re: Calling an action on init() Link to this post

    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.

  • CodeGuerrilla
    Avatar
    Community Member
    105 Posts

    Re: Calling an action on init() Link to this post

    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

    2051 Views
Page: 1
Go to Top

Want to know more about the company that brought you SilverStripe? Then check out SilverStripe.com

Comments on this website? Please give feedback.