SSbits - Home page
Site by Carbon Crayon
Submit a Post >

Snippets - Little bits of code to make you happy

Automatically Redirect to the first Child Page without a Redirector Page

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());
	}
}
  • Bart van Irsel
    03/10/2011 2:25pm (3 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 (3 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 (3 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 (3 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 (2 years ago)

    Useful in practice. Thanks.

Post a comment ...

You cannot post comments until you have logged in. Login Here.

Advertisement

Site of the Month

Find SSbits on

Top Contributers

Rank Avatar Name
1 article image Aram Balakjian
2 article image Daniel Hensby
3 article image Marcus Dalgren
4 article image Hamish Campbell
5 article image Ty Barho
6 article image njorndare
7 article image Martijn van Nieuwenhoven
8 article image Darren-Lee
9 article image Roman Schmid
10 article image Matt Clegg

View full leaderboard


Advertisement