Jump to:

23377 Posts in 18296 Topics by 2867 members

General Questions

SilverStripe Forums » General Questions » Dynamically create redirector page

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: 885 Views
  • DeklinKelly
    Avatar
    Community Member
    197 Posts

    Dynamically create redirector page Link to this post

    The following code creates a new page:

    $p = new Page();
    $p->Title = 'Hello';
    $p->MenuTitle = $p->Title;
    $p->URLSegment = $p->Title;
    $p->Content = '';
    $p->Status = 'Published';
    $p->write();
        $p->doRestoreToStage();
    $p->publish('Stage', 'Live');
    $p->flushCache();


    How can I create a redirector page that redirects to / ?

    I don't want a normal page, when someone goes to this URL I want them to be redirected to the home page.

  • Willr
    Avatar
    Forum Moderator
    5490 Posts

    Re: Dynamically create redirector page Link to this post

    How can I create a redirector page that redirects to / ?
    I don't want a normal page, when someone goes to this URL I want them to be redirected to the home page.

    Well instead of creating a new Page(); create a new RedirectorPage();

    You also then need to set the fields to point to the page it redirects to. Via

    $p->RedirectionType = 'Internal';
    $p->ExternalURLID = DataObject::get_one('SiteTree', "URLSegment = 'home'");

  • DeklinKelly
    Avatar
    Community Member
    197 Posts

    Re: Dynamically create redirector page Link to this post

    Perfect, thanks! Now how can I keep that item from showing up in the menu? I can't figure out how to use ShowInMenus with this.

  • Willr
    Avatar
    Forum Moderator
    5490 Posts

    Re: Dynamically create redirector page Link to this post

    $p->ShowInMenus = 0; // or false should also work.

  • pouic
    Avatar
    Community Member
    16 Posts

    Re: Dynamically create redirector page Link to this post

    How can I validate the urlsegment page already exists before creating the dynamic page?

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