Jump to content

Recommended Posts

Posted

Hi, community. 

I'm using a hook to path to change URL of some pages.

	wire()->addHook("Page(template=services-category|service)::path", function($e) {
		$page = $e->object;
		$e->return = "/{$page->name}/";
	});

Then in template where I enable URL segments, I use code like 

if (input()->urlSegment(1)) {
	$pagename = input()->urlSegment(1);
	$match = pages()->findOne("template=services-category|service, name={$pagename}");
	if (!$match->id) throw new Wire404Exception();
	echo $match->render();
	return $this->halt();
}

Typical. I have the template file for "service" template, so I can use $page->render() and because of it, these pages are also accessible from their old URLs.

So my question is how to prevent these pages being accessible from their original URLs without removing template file for their template?  

Eugene. 

Posted

Perhaps setting page template file in the hook or set alternative template file in admin?

  • Like 1
Posted

@tpr Thank you for suggestions.

Hook to what? 

Alternative template file does not do the job. Pages are still accessible by original URLs.

Posted
$url_array = explode("/", $_SERVER['REQUEST_URI']);
$parent_name = page()->parent->name;
if(in_array($parent_name, $url_array)) throw new Wire404Exception();

This code does the job but it feels a little bit dirty to me.

Posted

I haven't looked at the details here, but I wanted to mention that maybe you should be careful of the $parent_name in array check. Maybe parent->path would be safer. There is always the chance that you could end up with two pages on different branches with the same name. 

Also, remember that we have $input->url (instead of $_SERVER['REQUEST_URI']).

What about this:

if(strpos($input->url, page()->parent->path) !== false) throw new Wire404Exception();

59ecb69ca894f_ScreenShot2017-10-22at8_16_44AM.png.581f59ac42facd742119849f1200354d.png

  • Like 1
Posted

@adrian 

I think that I have to clarify my setup, so it would be easier for you to suggest something. 

Page tree

Home ( enabled URLsegments )

- Services section ( template: services-section | url: services-section ) 

-- Services category ( template: services-category | url: services-category ) 

--- Service ( template: service | url: service ) 

Your code will work in this case, but after you hook "path" for "service" and "services-category" templates it wouldn't.

wire()->addHook("Page(template=services-category|service)::path", function($e) {
		$page = $e->object;
		$e->return = "/{$page->name}/";
	});

So $page->parent->path and $input->url will return just "/services-section/" and  "/service/". 

Now, "service" page is accessible from two URLs site.com/service/ and site.com/services-section/services-category/service and I what to throw 404 if the second URL is meet.  But when accessing this URL $page->url returns "/services/".

I didn't find any Processwire method that can return actual URL ( original ), that why I use $_SERVER['REQUEST_URI']

 

 

Posted

I think the solution to your problem is rather simple: Set up your hooks after you check for unwanted urls. 

  • Like 1
Posted
12 hours ago, Zeka said:

So my question is how to prevent these pages being accessible from their original URLs without removing template file for their template?

At the top of your template file:

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

 

Posted
8 minutes ago, Zeka said:

Could you please clarify what is pageStack and is $options predefined variable in template files? 

The $options are used in the PageRender module. There isn't much documentation for them apart from what is in the module file.

Quote

pageStack: An array of pages, when recursively rendering. Used internally. You can examine it but not change it.

 

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