View more context

 

clodeindustrie

I must doing something ridiculous then, because I can't get any other action than index, to load

theruss

I almost always avoid these controller statics, and use YML-based routing ala

  1. SilverStripe\Control\Director:
  2. rules:
  3. 'health/check//$Action/$ID/$OtherID': My\Namespace\Control\HealthCheckController
clodeindustrie

is there any way to specify an action without it being in the URL though?

theruss

If a "variable" isn't incoming via a URL, then it has to come from $_GET|POST|COOKIE|SESSION

clodeindustrie

I might route it through index then I guess, I wanted to do triage based on the presence or not of a URL parameter

theruss

IMO "Cleaner" is defining configuration in YML, but then I''m just used to it that way 🙂

clodeindustrie

I'm not worried about doing it in YML or straight in the class, it's just I couldn't find a way to specify the action in the YML

clodeindustrie

so / would go to action1 and /$Param would go to action 2

clodeindustrie

it felt cleaner to use two different actions for two different ways of accessing the controller

theruss

Right, so if you wanted all incoming requests to "/" to be applied to a specific controller, you can do that:

  1. SilverStripe\Control\Director:
  2. rules:
  3. '/': My\Namespace\MyController
theruss

SilverStripe will then just use its default pattern-matching to extract parameters from the URL

clodeindustrie

meaning I have to specify $Action in the URL to load the corresponding action?

theruss

Then you can write a public method named for each entry in that array, and SilverStripe will call it for you.

theruss
  1. private static $allowed_actions = [
  2. 'foo',
  3. 'bar',
  4. ];
  5.  
  6. public function foo()
  7. {
  8. ...
  9. }
  10.  
  11. public function bar()
  12. {
  13. ...
  14. }
clodeindustrie

cool thanks, and can I make it so that foo is called when the page loaded is / and bar when the page loaded is /$myparam ?

theruss

Sort of. Any request to '/' will call index() there's no way to manually modify where requests to '/' go by default, other than through index(), but you can certainly write some additional "marshalling" logic within index() to perform custom stuff

theruss

If $param is dynamic, then there's no point creating a baked-in method, because the actual string used in the URL can change.

theruss

So we kinda go round in circles here. There's no way to accept a "flexible" or "non-defined" param. SilverStripe (or any system) has to "hook into" something

theruss

Hence the $url_handlers static, which allows you to declare methods based on that array's values

theruss

Have a really good read of that docs page. I've been doing this for ~10 years and I still need to refer to that 🙂 sometimes

clodeindustrie

alright, thanks for taking the time to coach me through this


Show less replies
Aaron Cooper
  1. $registration = MyObject::create();
  2. $form->saveInto($registration);
  3. $registration->write();
Aaron Cooper

Does $form need vars removed from it that don't match anything in $registration?

Aaron Cooper

I'm having a record created for $registration, but all fields are null. Yet they are all populated in $form->requestParams