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.

We've moved the forum!

Please use forum.silverstripe.org for any new questions (announcement).
The forum archive will stick around, but will be read only.

You can also use our Slack channel or StackOverflow to ask for help.
Check out our community overview for more options to contribute.

General Questions /

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

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

Dynamically create redirector page


Go to End


5 Posts   1546 Views

Avatar
DeklinKelly

Community Member, 197 Posts

6 January 2011 at 4:18pm

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.

Avatar
Willr

Forum Moderator, 5523 Posts

6 January 2011 at 8:43pm

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'");

Avatar
DeklinKelly

Community Member, 197 Posts

7 January 2011 at 8:35am

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.

Avatar
Willr

Forum Moderator, 5523 Posts

7 January 2011 at 6:56pm

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

Avatar
pouic

Community Member, 19 Posts

28 July 2011 at 11:36am

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