Skip to content

Instantly share code, notes, and snippets.

@gebeer
Last active March 29, 2017 06:38
Show Gist options
  • Save gebeer/bfbec133e365c4129af161b48e649a95 to your computer and use it in GitHub Desktop.
Save gebeer/bfbec133e365c4129af161b48e649a95 to your computer and use it in GitHub Desktop.
landingpages that live off root ProcessWire

https://processwire.com/talk/topic/9748-pages-to-have-absolute-path/?do=findComment&comment=93713

sometimes clients need landing pages for marketing/seo that need to live off the root.

all of these pages would make a mess in the page tree, so they are stored under /landing-pages/

I always do a hook (as was demonstrated in the CMS Critic Case Study)

wire()->addHookBefore('Page::path', function($event) {
  $page = $event->object;
  
  if($page->template == 'landing-page') {
    // ensure that pages with template 'landing-page' live off the root
    $event->replace = true;
    $event->return = "/$page->name/";
  }
  
});

on the landing page template:

// if someone tries to access this page at it's real location in the page tree, redirect to the fake URL:
if(!$input->urlSegment1) {
	$session->redirect($page->url);
}

i should also add that on the homepage there is this:

/**
 * First, check to see if there is a URL segment off the homepage
 * this is used to generate custom landing pages, for marketing etc..
 * with a shorter URL, living off the root.
 *
 */

if(strlen($input->urlSegment2)) {
  // we only accept 1 URL segment here, so 404 if there are any more
  throw new Wire404Exception();

} else if(strlen($input->urlSegment1)) {
  // render the landing page named in urlSegment1 
  $name = $sanitizer->pageName($input->urlSegment1);
  $landingPage = $pages->get(2281)->child("name=$name, include=hidden");
  
  if($landingPage->id) echo $landingPage->render();
    else throw new Wire404Exception();

} else { // do the normal homepage...

so that's why the segment check on the landing page template, because when it is being rendered from the homepage, if(!$input->urlSegment1) will be true

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment