Jump to content

[resolved] Hiding parent url error


blacksrv
 Share

Recommended Posts

Hi, following the instructions from this post https://processwire.com/talk/topic/3987-cmscritic-development-case-study/?p=36867.

I added a hook to my site to replace the parent URL, when I access to the new url I always get a 404, what I'm missing?

hook:

wire()->addHookBefore('Page::path', function($event) {
	$page = $event->object;
	if($page->template == 'articulo-carrera') {
	  	$event->replace = true;
	    $event->return = "/$page->name/";
	}
}); 
Link to comment
Share on other sites

In which file have you placed that code?

Exactly like the post quoted:

1) config.php

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

2) _init.php

require_once("./includes/hooks.php");

3) hooks.php

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

Thanks

  • Like 1
Link to comment
Share on other sites

You're missing the /site/templates/home.php

You only modify the url the echo $yourpost->url would output thus leading you to the modifed url off root. But the system or the root doesn't know about that, it looks for a /pagename/ but it doesn't exists so you have to implement it and capture the urlSegments etc. 

This is common thinking that a hook will change PW's routing but it doesn't. It just modifies output of the page's url.

Link to comment
Share on other sites

You're missing the /site/templates/home.php

You only modify the url the echo $yourpost->url would output thus leading you to the modifed url off root. But the system or the root doesn't know about that, it looks for a /pagename/ but it doesn't exists so you have to implement it and capture the urlSegments etc. 

This is common thinking that a hook will change PW's routing but it doesn't. It just modifies output of the page's url.

Thank you, Now I get it.

Something I notice it's important to have  Allow URL Segments? checked.

Code I'm using if someone is looking for the same:

if(count($input->urlSegments) == 1) {
    $home = false;
    $seg1 = $sanitizer->pageName($input->urlSegment1);
    $found = $pages->get("template=articulo-carrera, name=$seg1");
    if($found->id) echo $found->render();
}
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

  • Recently Browsing   0 members

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