Skip to main content

This site requires you to update your browser. Your browsing experience maybe affected by not having the most up to date version.

General Questions /

General questions about getting started with SilverStripe that don't fit in any of the categories above.

Moderators: martimiz, Sean, biapar, Willr, Ingo, swaiba, simon_w

Controller with only $ID, no $Action


Reply


3 Posts   364 Views

Avatar
Sygmoral

Community Member, 29 Posts

24 April 2014 at 3:19am

Edited: 24/04/2014 3:20am

I have a page called video. I want /video/abc123 to play a video with ID abc123.

I also want the page 'video' to be translatable. So it won't always be the exact word 'video'.

I previousy had it working when it didn't have to be translatable yet, but right now I'm forced to make the URL longer: /video/yt/abc123. That's because I'm using the standard $Action/$ID/$Parameter pattern, allowing me to translate the page name (can't figure out how to do it in the config otherwise). So in the example /video/yt/abc123, yt is my "action", which I have set to allowed.

But I would like to go back to just /[PageTitle]/abc123. I tried getting that to work by using the $Action as the video ID, but that doesn't work because it just says Page not Found then (despite 'allowing all actions'), because it doesn't know I'm trying to do something with the parameter rather than making it load a page. I suppose 'allow all actions' only works for functions that have actually been defined, and of course I don't have a function for each video ID.

Is there a way around this? Perhaps something with routes, but how to get around the translated page name then? Or can I in another way force the url handler to stop looking for pages, and push all parameters to just that video page (with no action checks)?

Avatar
Devlin

Community Member, 225 Posts

25 April 2014 at 3:15am

Edited: 25/04/2014 3:21am

class VideoPage_Controller extends Page_Controller {
   
   private static $url_handlers = array(
      '$ID!' => 'show',
   );
   private static $allowed_actions = array(
      'index',
      'show',
   );

   function show() {
      $params = $this->getURLParams();

      if (!empty($params['ID']) && is_numeric($params['ID'])) {
         // get video by id
      }
      
      return $this->httpError(404);
   }
}

http://doc.silverstripe.org/framework/en/topics/controller#url-handling

Avatar
Sygmoral

Community Member, 29 Posts

25 April 2014 at 3:58am

Excellent, thank you! I didn't immediately get it to work, but a flush solved that (should be obvious, I know). It didn't actually need the 'index' in the allowed_actions array.

I had not understood that documentation completely because the first part of all the patterns that are mentioned, are always actions that are in the allowed_actions array. In my opinion, the drivethrough example there doesn't even make sense, because 'drivethrough' is an allowed action, while the function being called is order, not drivethrough! Right? Someone should probably change that bit in the documentation then.

Thank you for clearing it up with your answer :)