Jump to:

23368 Posts in 18145 Topics by 2863 members

General Questions

SilverStripe Forums » General Questions » Static URLSegment?

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: 849 Views
  • UncleCheese
    Avatar
    4085 Posts

    Static URLSegment? Link to this post

    Is there a way to have a static URLSegment for a given page? Sometimes I have to hardcode URLSegments into my template, and, although it's unlikely, it would be nice if I could be sure the client wouldn't change the title and mess things up.

  • banal
    Avatar
    Community Member
    901 Posts

    Re: Static URLSegment? Link to this post

    Hi UncleCheese.
    Well. You could use the Page ID, since that's immutable by your client. But this would fail if the client deletes the page and tries to re-create it.
    In the getCMSFields function you could make the ID visible for the user inside the CMS:

    $fields->addFieldToTab(
       'Root.Content.Main',
       new ReadonlyField('PageId', 'Page ID', $this->ID)
    );

    And in the template you would do something like:

    <% if ID = 3 %>
    .. specific content for page with id 3 here ...
    <% end_if %>

    Another alternative could be a field that can only be written once.
    Here's an example how this could be done:

    public static $db = array(
       'MyIDField' => 'Varchar(64)'
    );

    public function getCMSFields() {
       $fields = parent::getCMSFields();
       if($this->MyIDField == null){
          $fields->addFieldToTab(
             'Root.Content.Main',
             new TextField('MyIDField', 'Page-ID')
          );
       } else {
          $fields->addFieldToTab(
             'Root.Content.Main',
             new ReadonlyField('MyIDField', 'Page-ID')
          );
       }
       return $fields;
    }

    849 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.