Zeka Posted October 22, 2017 Share Posted October 22, 2017 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. Link to comment Share on other sites More sharing options...
tpr Posted October 22, 2017 Share Posted October 22, 2017 Perhaps setting page template file in the hook or set alternative template file in admin? 1 Link to comment Share on other sites More sharing options...
Zeka Posted October 22, 2017 Author Share Posted October 22, 2017 @tpr Thank you for suggestions. Hook to what? Alternative template file does not do the job. Pages are still accessible by original URLs. Link to comment Share on other sites More sharing options...
Zeka Posted October 22, 2017 Author Share Posted October 22, 2017 $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. Link to comment Share on other sites More sharing options...
adrian Posted October 22, 2017 Share Posted October 22, 2017 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(); 1 Link to comment Share on other sites More sharing options...
Zeka Posted October 22, 2017 Author Share Posted October 22, 2017 @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'] Link to comment Share on other sites More sharing options...
abdus Posted October 22, 2017 Share Posted October 22, 2017 I think the solution to your problem is rather simple: Set up your hooks after you check for unwanted urls. 1 Link to comment Share on other sites More sharing options...
Robin S Posted October 22, 2017 Share Posted October 22, 2017 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(); Link to comment Share on other sites More sharing options...
Zeka Posted October 22, 2017 Author Share Posted October 22, 2017 @Robin S Thank you! Could you please clarify what is pageStack and is $options predefined variable in template files? Link to comment Share on other sites More sharing options...
Zeka Posted October 22, 2017 Author Share Posted October 22, 2017 @Robin S Thanks once again, your solution works! Don't know how I missed that we have $options variable in templates Link to comment Share on other sites More sharing options...
Robin S Posted October 22, 2017 Share Posted October 22, 2017 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. 1 Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now