4

I am working on a custom email form that should be able to handle the following URLs:

http://www.example.com/email

Defaults to general email address.

http://www.example.com/email/Office/1

Gets email contact details from Office model, ID 1

http://www.example.com/email/Staff/96

Gets email contact details from Staff model, ID 96

I thought I could do this by accessing the normal $Action/$ID variables - but got utterly confused by the Routing documentation: https://docs.silverstripe.org/en/3.3/developer_guides/controllers/routing/

class EmailPage_Controller extends Page_Controller
{

  private static $allowed_actions = array(
    'Form',
    'Staff',
    'Office'
    );

  public function Form() { 

    $Action = $this->request->getVar('Action');
    $ID = $this->request->getVar('ID');  

Doesn't work.

What's the best way of accessing/ passing URL variables to a form in Silverstripe?

BaronGrivet
  • 4,364
  • 5
  • 37
  • 52

1 Answers1

5
$ID = $this->request->param('ID');

This will get you the id from the Url parameters as you have defined them in your routes or $url_handlers.The same method is used to get the action parameter.

Ossama
  • 89
  • 3
  • That's exactly what I needed to do. I normally used param() but must of copy/pasted getVar() from some other code. – BaronGrivet Jun 13 '16 at 01:19