Jump to:

22977 Posts in 11742 Topics by 2826 members

General Questions

SilverStripe Forums » General Questions » Director::AddRules

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: 1449 Views
  • ccburns
    Avatar
    Community Member
    74 Posts

    Director::AddRules Link to this post

    My apologies if this is already explained somewhere, but I have tried to find the solution but to no avail.

    I am trying to extend the URL params that are allowed for a particular controller on a new site I am building

    http://domainname.local/my-zone/body/calendar/10/2011/
    http://domainname.local/pagename/$Action/$ID/$OtherID/?/

    This leaves me one parameter short and rather than just use a ?year=2011 I wanted to learn how to add another parameter to the URL.

    In this set up my-zone is a page and so is body.

    Body has a pagetype of WeighIn and calendar is a method on the WeighIn_Controller

    I have tried adding the Rule to Director in a number of different ways but no luck, so I guess my understanding of it is completely wrong. Would appreciate any help.

    Cheers,
    Colin

    /mysite/_config.php

    Director::addRules(100, array('/my-zone//$Action/$ID/$OtherID/$Year'=>'WeighIn_Controller'));

    /mysite/WeighIn.php

    <?php

    class WeighIn extends Page {

       public static $db = array(
       );

       public static $has_one = array(
       );

    static $icon = "/themes/dd/images/treeicons/user_orange";

    }
    class WeighIn_Controller extends Page_Controller {

    public static $allowed_actions = array (
    'entry',
    'addentry',
    'saveentry',
    'calendar',
    'list'
    );

    public function init() {
    parent::init();
    }

    /***************************************
    * Add Weigh In Point
    */
    function addentry() {
    // form code
    }

    function saveentry($data, $form) {
    // Save code
    }

    /* draws a calendar */
    function draw_calendar($month,$year){

    // code to draw calendar

    }

    function CalendarHTML($month = null, $year = null) {

    echo "Year = ".Director::urlParam('Year')."<hr/>";
    echo "Action = ".Director::urlParam('Action')."<hr/>";
    echo "ID = ".Director::urlParam('ID')."<hr/>";
    echo "OtherID = ".Director::urlParam('OtherID')."<hr/>";

    if($month === null){
    $month = date("n");
    }
    if($year === null){
    $year = date("Y");
    }

    return $this->draw_calendar($month, $year);

    }

    }

  • Ryan M.
    Avatar
    Community Member
    309 Posts

    Re: Director::AddRules Link to this post

    It doesn't look like you've built a function to handle the $Action. For example,

    http://domainname.local/my-zone/body/calendar/10/2011 => pagename//$Action/$Type/$Month/$Year
    ( I think you need to remove the slash at the beginning of the url route )

    public function body() {
    this 'body' function is matched with the $Action in the url route.
    then inside this function you can use the rest of the url parameters to bring up a calendar set to the $Month and $Year.
    }

    More Info: http://www.ssbits.com/tutorials/2010/dataobjects-as-pages-part-2-using-model-admin-and-url-segments-to-create-a-product-catalogue/

  • ccburns
    Avatar
    Community Member
    74 Posts

    Re: Director::AddRules Link to this post

    Opps sorry Ryan I see my mistake in the question.

    There is a page called "my-zone" and then a subpage with a PageType of WeighIn which is called Body.

    So where I typed

    http://domainname.local/my-zone/body/calendar/10/2011/
    http://domainname.local/pagename/$Action/$ID/$OtherID/?/

    it should have been

    http://domainname.local/my-zone/body/calendar/10/2011/
    http://domainname.local/pagename/subpagename/$Action/$ID/$OtherID/?/

    So I have a template named WeighIn_calendar.ss and this is all working correctly, just trying add in a new parameter that I can access. Obviously I could just use the query string, but I am just trying to learn how to do it without the query string

    I will take a look at the link you sent for SSBITS.

    Thanks for your help, hopefully this makes it a little clearer

    Colin

    I will take a look at the

  • Willr
    Avatar
    Forum Moderator
    5462 Posts

    Re: Director::AddRules Link to this post

    You won't need to mess around with Director::addRules for that, $url_handlers on your page_controller can give names to more params.

    Mark Rickerby (@maetl) has a good guide to URL handling in SilverStripe - http://maetl.net/silverstripe-url-handling.

  • ccburns
    Avatar
    Community Member
    74 Posts

    Re: Director::AddRules Link to this post

    Thanks Willr & Ryan M,

    I now have it all sorted. One of my issues was that I was trying use the template WeighIn_calendar.ss and therefor there wasn't necessarily a calendar() method.

    So in the end this is the code I have if anyone else is looking

    public static $url_handlers = array(
    'calendar/$Type/$Month/$Year' => 'calendar',

    );

    ...

    function calendar($request) {

    $month = $request->param('Month');
    $year = $request->param('Year');
    $type = $request->param('Type');

    if($type == 'calendar'){
    return $this->draw_month_calendar($month, $year);
    }elseif($type == 'list'){
    return $this->draw_month_list($month, $year);
    }

    }

    1449 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.