Jump to:

23481 Posts in 18965 Topics by 2878 members

General Questions

SilverStripe Forums » General Questions » Controller with only $ID, no $Action

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

Page: 1
Go to End
Author Topic: 233 Views
  • Sygmoral
    Avatar
    Community Member
    23 Posts

    Controller with only $ID, no $Action Link to this post

    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)?

  • Devlin
    Avatar
    Community Member
    215 Posts

    Re: Controller with only $ID, no $Action Link to this post

    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

  • Sygmoral
    Avatar
    Community Member
    23 Posts

    Re: Controller with only $ID, no $Action Link to this post

    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

    233 Views
Page: 1
Go to Top

Want to know more about the company that brought you SilverStripe? Then check out SilverStripe.com

Comments on this website? Please give feedback.