Jump to content
Zeka

URLsegments, path hook and original URL

Recommended Posts

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. 

Share this post


Link to post
Share on other sites

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

  • Like 1

Share this post


Link to post
Share on other sites

@tpr Thank you for suggestions.

Hook to what? 

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

Share this post


Link to post
Share on other sites
$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.

Share this post


Link to post
Share on other sites

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

Share this post


Link to post
Share on other sites

@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']

 

 

Share this post


Link to post
Share on other sites

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

  • Like 1

Share this post


Link to post
Share on other sites
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();

 

Share this post


Link to post
Share on other sites

@Robin S Thank you!

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

Share this post


Link to post
Share on other sites

@Robin S

Thanks once again, your solution works! 

Don't know how I missed that we have $options variable in templates:-[

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.


  • Recently Browsing   0 members

    No registered users viewing this page.

×
×
  • Create New...