Snippets - Little bits of code to make you happy
Automatically Redirect to the first Child Page without a Redirector Page
Tweet27 September 2011 | | | Supports v2.4
This is a little snipped i've been using to redirect a custom page to it's first child. It extends the init method of the Controller. This has proven useful in cases where the client wants to re-sort the child pages and doesn't want to mess around the configuration of a redirector page.
function init() {
parent::init();
if($this->Children()->Count()){
Director::redirect($this->Children()->First()->AbsoluteLink());
}
}
6 Comments
RSS feed for comments on this page RSS feed for all comments
Bart van Irsel
03/10/2011 2:25pm (2 years ago)
Hi ekersten,
Thanks for this tip, i use a ChildRedirectorPage for this which also links directly to the firstchild when $Link is used in the temaplte system.
I placed it here, maybe more people will like it:
http://www.sspaste.com/paste/show/4e89bf54c5fa3
vani
04/10/2011 5:16pm (2 years ago)
oh thank you very much for the code have been searching for a long time found it here finally<a href="http//www.iphone5mobile.blogpost.com/">iphone 5 spcifications</a>
vani
04/10/2011 5:17pm (2 years ago)
oh thank you very much for the code have been searching for a long time found it here finally<a href="http://www.iphone5mobile.blogpost.com/">iphone 5 specifications</a>
moloko_man
04/10/2011 5:22pm (2 years ago)
If you have hidden children pages, and you want to forward to them you will need to use:
$this->AllChildren()->First()->AbsoluteLink()
Nick Jacobs
06/11/2011 9:32pm (2 years ago)
Great tip!.... I'd do a test on this so that it only redirects if the current page has no content...something like this (not tested):
if(!$this->dbObject('Content')->hasValue()){
if($this->Children()->Count()){
Director::redirect($this->Children()->First()->AbsoluteLink());
}
}
}
Adil Aliyev
28/01/2012 9:47pm (1 year ago)
Useful in practice. Thanks.
Post a comment ...
You cannot post comments until you have logged in. Login Here.