URL: Hyphens auto mapped to underscores?

Silverstripe Version: 4.1

This works great I’m just a little confused as I can’t see any docs on it.

I have a Controller: RetailController.

I have a route for it: ‘retail’: ‘RetailController’

In this controller I have a function signage_yes. I don’t have a route for it.

I do have ‘signage_yes’ in $allowed actions.

A url of retail/signage-yes does work. It finds the right controller and method despite one is hyphen and one is underscore.

So that’s all good, saves me me writing a grip of routes but why does this work and is it expected/reliable?

Edit: here’s a debug_request on it:

Debug (line 261 of RequestHandler.php): Testing ‘$Action//$ID/$OtherID’ with ‘signage-yes’ on RetailController

Debug (line 269 of RequestHandler.php): Rule ‘$Action//$ID/$OtherID’ matched to action ‘handleAction’ on RetailController. Latest request params: array ( ‘Action’ => ‘signage-yes’, ‘ID’ => NULL, ‘OtherID’ => NULL, )

I don’t have a signage-yes function in the controller.

Hi Lance,

Yes that is the expected behaviour and it is reliable. The reason it works that way is that PHP method names can’t contain hyphens, and an underscore is the closest replacement.


1 Like

Great thanks Loz. I knew about the hyphens and PHP names - been a hassle in other frameworks, nice it’s auto handled in SS :+1: