BaronGrivet
BaronGrivet

Reputation: 4424

How to run no action and a single variable in the Silverstripe url_handlers?

I'm working on a project which has a special page type which checks a single 250 alphanumeric string.

Ideally I'd like the URL structure to work like this: http://www.example.com/check/I1gdTVUsnezY9SDI8V0GS2mg7Y0IdG6MqjCZ8t1yejRdi0pKzyr7G28iF0fyxOW9Le9vg3op7NnuCE0unT7d09aN00Trn7xPYAjLRhqQ9k5aRlsThsTk0HaS966MCDb4aC23RW4Cl273e9YiWKFNm2STI75X1jnlZ684M7ejDpmWg1YfM32OpwX066bF5VTp5v0F5I42T2SWh8QhMc9GW9I2ZbuP7ykh710UHnLwQyA3BO7KitZWcCU0u9

However using allowed_actions and url_handlers the standard way I can only get it to work if I preface the alphanumeric string with "uid" - http://www.example.com/check/uid/string-goes-here

class CheckPage_Controller extends Page_Controller {

  private static $allowed_actions = array(
      'uid'
  );
  private static $url_handlers = array(
      'uid/$uID' => 'uid'
  );

Is it possible to have url_handlers work with just a variable and no action on a custom page?

Upvotes: 0

Views: 78

Answers (2)

wmk
wmk

Reputation: 4626

Assuming you have a pagetype "CheckPage" with $URLSegment "check" and some Dataobjects in a has_many relation you can use Nightjars extension for using this urlslug method, which is really elegant:

https://github.com/NightJar/silverstripe-slug/

ATM it's no ready to install module but an extension to the controller you can configure.

If you need any further help please provide some more informations about your code structure.

Upvotes: 0

spekulatius
spekulatius

Reputation: 1499

You can define a route without a action in your _config.php:

Director::addRules(100, array(
    'check/$UID' => 'CheckPage_Controller'
));

and in your CheckPage_Controller you can catch the request in the index function:

public function index() {
    var_dump($this->request->allParams());die;
}

But you should be aware that the add route catches all requests to /check/whatever. So you need to define a different url for other stuff.

Upvotes: 1

Related Questions