hi, new to routing in SS, I have two actions, I would like one to receive people loading
/ and another one to receive
/$param I have used the following but it's not working:
- private static $url_handlers = [
- '/$param!' => 'action1'
Hoping that people loading
/ will land in
I always end up on
mm I'm using
PageController that might play out
I think it needs to be
- '$Action/$param!' => 'action1'
I must doing something ridiculous then, because I can't get any other action than index, to load
I almost always avoid these controller statics, and use YML-based routing ala
- 'health/check//$Action/$ID/$OtherID': My\Namespace\Control\HealthCheckController
is there any way to specify an action without it being in the URL though?
Well, then it's not an action any more.
If a "variable" isn't incoming via a URL, then it has to come from $_GET|POST|COOKIE|SESSION
I might route it through index then I guess, I wanted to do triage based on the presence or not of a URL parameter
You can absolutely do that in YML.
IMO "Cleaner" is defining configuration in YML, but then I''m just used to it that way 🙂
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
/ would go to action1 and
/$Param would go to action 2
without mention of
action1 in the URL
it felt cleaner to use two different actions for two different ways of accessing the controller
Right, so if you wanted all incoming requests to "/" to be applied to a specific controller, you can do that:
- '/': My\Namespace\MyController
SilverStripe will then just use its default pattern-matching to extract parameters from the URL
meaning I have to specify
$Action in the URL to load the corresponding action?
Ah, now I see what you mean!
You want to configure
Then you can write a public method named for each entry in that array, and SilverStripe will call it for you.
- private static $allowed_actions = [
- public function foo()
- public function bar()
cool thanks, and can I make it so that foo is called when the page loaded is
/ and bar when the page loaded is
/ being the root of my url segment
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
$param is dynamic, then there's no point creating a baked-in method, because the actual string used in the URL can change.
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
Hence the $url_handlers static, which allows you to declare methods based on that array's values
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
alright, thanks for taking the time to coach me through this