Jump to content

Recommended Posts

Posted

I know this issue has come up a few times before, I've read those posts, I've tried the technique but I must be missing something. Plus those posts are years old and maybe PW has different hooks and functions now?

Anyway, this site has a page that has existed for a long time under root, i.e. /supercool.

The page also has 1 and even 2 layers of subpages like /supercool/athing/apost.

In other words, it's a big and important section of the site.

But the site is growing and we have to consolidate and organize pages, menus etc to be more efficient. So supercool is going to be moved under a new /resources/ page.

The problem is we want the URL to not change, nor for all the subpages. We still want /supercool/athing and not switch to /resources/supercool/athing/apost.

I tried using the wire()->addHookBefore('Page::path..... method and this does change the path of all the links found on the website. In other words, a link in the footer at first changed to /resources/supercool but after using the hook it now says /supercool/ again, great! But it won't render, I just get a 404 page.

I tried the technique found here to add some logic to the home page template but it does nothing, like it's ignoring the tests for segments entirely.

 

So that said, the Page::path hook technique is working to change the URLs as found across the site, but I just get a 404 on actual page load.

How do I complete this so that /supercool/ will load from its new URL location?

Posted
3 hours ago, Vigilante said:

add some logic to the home page template but it does nothing, like it's ignoring the tests for segments entirely.

Did you enable urlSegments for the home template? Setup > Templates > home > Urls

Posted

After posting this question and continuing to search for answers, I did come across one passing statement to that effect. I don't know why this isn't better documented somewhere.

I did turn on segments and then coded some logic to pick the right page to render.

For anybody curious here is what I ended up doing.

First, I added a universal file to be loaded before any template, using the site/config.php variable:

$config->prependTemplateFile = '_init.php';

 

Then in _init.php I added the hook:

wire()->addHookAfter('Page::path', function($event) {
    $page = $event->object;
    // Array key is slug to be removed, value is subpage for which to remove it.
    $pagesForRoot = [
        '/aparent' => 'adjusted-page',  // Remove /aparent but only for adjusted-page subpages
        // add new slugs and pages as needed
    ];
    $the_url = $event->return;
    foreach($pagesForRoot as $url => $subpage) {
        if(strpos($the_url, $url) !== false && strpos($the_url, $subpage) !== false) {
            if (str_replace("/", "", $the_url) != str_replace("/", "", $url)) {
                $the_url = str_replace($url, "", $the_url); // Sanitized without slug
            }
            $event->return = $the_url; // Possibly return original slug if it's the landing page
        }
    }
});

 

Then in the template where the segments are controlled I added some logic:

if($input->urlSegment1 == "adjusted-page") {
    if(count($input->urlSegments) > 1) {
        $path = $input->urlSegmentStr;
        $post = $pages->get("/removed-slug/".$path."/");        
    } else {
        $post = $pages->get("/removed-slug/".$input->urlSegment1."/");
    }
    if($post->id) {
        echo $post->render();
    } else {
        throw new Wire404Exception();
    }
} else {
    // normal output of the page
}

 

I feel like the code could be better, there is disconnect between the logic in the _init file and then the page controller. Like I have to adjust for my removed slugs twice or something.

If I add any more removed slugs, the logic in the page is going to start getting silly. But this is a start, and it works.

  • Thanks 1

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
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...