Jump to content

[solved] Urls without parents, double content?


rash
 Share

Recommended Posts

Hi guys,

I'm a Processwire-Newbie and new to this forum. Happily I have to struggle with very few difficulties, thanks to the clear and pleasing concept and structure of PW. Currently there is only one thing that makes me brood:

I have a main category 'posts' that contains the majority of all pages. So the regular url would be 'domain/posts/post-one' etc. As I prefer the url scheme 'domain/post-one' I followed the instructions discussed in this topic.

This hook is in my 'init.php':

wire()->addHookBefore('Page::path', function($event) {
  $page = $event->object;
  if($page->template == 'post') {
    $event->replace = true;
    $event->return = "/$page->name/";
  }
});

And this is in my 'home' template:

if(strlen($input->urlSegment2)) {
    throw new Wire404Exception();
}
else if(strlen($input->urlSegment1)) {
    $input->urlSegment1;
    $name = $sanitizer->pageName($input->urlSegment1);
    $post = $pages->get("/posts/")->child("name=$name");      
    if($post->id) echo $post->render();
    else throw new Wire404Exception();
      $renderMain = false;
} else {
  // regular homepage output
  $posts = $pages->find("parent=/posts/, limit=$limit, sort=-date");
  $body = renderPosts($posts);
}

Both templates 'home' and 'post' have 'url segments' option activated.

On the first sight everything is working fine. $page-name outputs '/domain/post-one' and the page '/domain/post-one' ist getting displayed. What's frighening me is the fact, that 'domain/posts/post-one' is working as well. This means 'post-one' can be adressed with two different urls, and I’m not sure how to rate that.

On one hand, nobody will ever notice the '/domain/posts/page-one' option, as it's listed nowhere. So I could just ignore it. On the other hand, I don't know for sure if this presumption is correct. Maybe there are unknown channels where the 'wrong' urls will be spreaded, then there will be 'doubled content' which is bad, as far as  I know.

So what I'm asking for: Is there an easy way to avoid the double url-scheme option and output a 404 error, when 'domain/posts/page-one' is called? Or should I just don't care, as it doesn't matter a all? Unfortunately, I don’t fully understand every line of the second code, so I would be very grateful if someone could light it up for me a bit.

Thanks + regards
Ralf

Link to comment
Share on other sites

Rename your post.php to _post.php and instead of $post->render() use wireRenderFile("_post", ['page' => $post]).

This way processwire doesn't automatically recognise that post does have a template file and therefore throws a 404 for those pages.

  • Like 3
Link to comment
Share on other sites

Your link to the topic you followed doesn't work, and I'm not sure what's in your renderPosts() function, but assuming that LostKobrakai is right and you are using $page->render() (edit: I see the render() call now), another alternative is to place this at the top of the template you render:

if( empty($options['pageStack']) ) throw new Wire404Exception();

 

  • Like 4
Link to comment
Share on other sites

@LostKobrakai: Thanks a lot for your fast help. I tried your suggestion and am at least a bit further. When I call 'domains/posts/post-one' a 404 is thrown out. Foolishly,  '/domain/post-one' doesn't show up anymore, instead 'domain' – the homepage – is displayed. As I said: I don't really understand what I'm doing ???

Link to comment
Share on other sites

@LostKobrakai: No, I don’t use $page-> viewable – there must be another issue.

@Robin S & diogo: I filled in the line you suggested and now everything works the way I was searching for. (Okay, I don’t have a clue what the line actually does, but as a newbie there must be some learning potential left.)

So big thanks to all of you for your kind answers, you were enormously helpful.

Ralf

  • Like 2
Link to comment
Share on other sites

$options is an always populated array in template scope.

And if there is no pageStack defined ($options["pageStack"]), it is a direct call to this page, what you don't want allow.

Welcome to PW and the forums! :)

  • 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...