Jump to content

pages to have absolute path


thfrlw
 Share

Recommended Posts

Hey everyone,
Im new to process wire.

Been doing some practice on this cms, and ran into problem,
I have several pages, and some have children pages.

Is there a way to define absolute path for children pages ?

Lets say I have

/

/services/

/services/some-service

can i set page /services/some-service/ path to be /some-service ?

to loose parent path prefix from it ?

Link to comment
Share on other sites

Hi, and welcome to the forum, and to PW!

I'm not sure why you'd want to do that - it really doesn't sound logical to me. That said, I don't think it's possible as I understand PW is very reliant on tree-structures (in other words, they are what they are). If there is a way of doing it, I'm sure someone else will be able to point you in the right direction.

Would it not be viable for you to just put your service pages under the root?

Link to comment
Share on other sites

As far as I know, there is no built-in way to do this. You would have to use a module or write one yourself. I found this post from ryan to be very helpful on that topic:
https://processwire.com/talk/topic/1799-routes-and-rewriting-urls/#entry16754

In this thread you'll also find further information about rewriting urls.

If you're rewriting urls there will certainly be side effects like the link module in ckeditor will not work properly anymore.

  • Like 1
Link to comment
Share on other sites

There are basically two ways of making a page accessible under a different url. Either you use urlSegements and a dedicated template, which then renders the page instead of own content.

// render another page in a tamplate
$pages->get("/services/some-service")->render();

The other option is hooking Page::path to modify the path on the fly. You can see an example here: https://gist.github.com/LostKobrakai/9c02484a9710e16bc512

Also this isn't a new issue, see here: https://processwire.com/talk/topic/9692-hidemask-a-parent-page-from-front-end/

  • Like 3
Link to comment
Share on other sites

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

  • Like 12
Link to comment
Share on other sites

  • 3 months later...
  • 1 year later...
On 4/24/2015 at 10:27 PM, Macrura said:

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/";
  }
  
});

This hook didn't work for me. Maybe something in the API has changed?

Reading up on hooks that modify the return value, I found that only After hooks can modify it.

I modified @Macrura 's code and this is working for me on 3.0.42 

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

 

  • Like 1
Link to comment
Share on other sites

5 hours ago, gebeer said:

Reading up on hooks that modify the return value, I found that only After hooks can modify it.

Only after hooks have access to the old return value, so to modify it you need to use an after hook. But you should be able to hook before a hookable function and return something completely custom. At least when making sure you do use $event->replace = true, because otherwise the hookable function would run as well and replace your custom return value.

  • Like 1
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...