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.

General Questions /

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

Director::AddRules


Reply


5 Posts   2174 Views

Avatar
ccburns

Community Member, 74 Posts

30 September 2011 at 6:25pm

Edited: 30/09/2011 6:27pm

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);

}

}

Avatar
Ryan M.

Community Member, 309 Posts

1 October 2011 at 8:02am

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/

Avatar
ccburns

Community Member, 74 Posts

1 October 2011 at 5:42pm

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

Avatar
Willr

Forum Moderator, 5513 Posts

1 October 2011 at 6:26pm

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.

Avatar
ccburns

Community Member, 74 Posts

1 October 2011 at 11:42pm

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);
}

}