Jump to content

redirect template without a .php file to it's parent?


John W.
 Share

Recommended Posts

Good day.

 

Short Version:  How can I redirect a template that doesn't have a .php file back to its parent?

-------

 

Detailed Version Question:

I have a a template that doesn't have a php file. The template is basically used to hold other templates. For instance, I have a php file called business-directory.php which display's all (child) businesses from the business categories template.

Business Directory

  • business-categories (2)
  1. ABC Fence
  2. Roper Cattle

So, if a user goes to /home/business-directory/ it fetches and displays all children from business categories.

However, if someone manually types in /home/business-directory/business-categories/ I want to redirect them back to the parent 'business-directory'. (without having to create a .php file called business-categories.php)

I was looking at the template settings for 'business-categories' and came up with what you see in the photo. I basically toggled off View Pages for guests to get the URL options, saved it, then toggled back on View Pages for guest (everyone) and it still retains the URL for the redirect. 

This below seems to work as long as the page is not moved. I tried putting in $page->parent->url;, but got an error that this was a 'Forged Request'.

 

Screen Shot 2016-06-27 at 2.14.27 PM.png

Any suggestions?

 

 

 

Link to comment
Share on other sites

  1.  I do not know why do you need it? Why not have 404 on something you don't want to show anyway?
  2. Why not have a php file to do what you need if you need it?
  3. You can build a separate page tree branch just for categories (for business-categories template pages) and connect them to end-level pages like ABC Fence with page field. In this case your end-level pages should have different template and can live under business-directory, while categories themselves are elsewhere as noted above.
  • Like 2
Link to comment
Share on other sites

you could do a hook that rewrites the URL for those child pages to the parent.. doing it 'all the time'
 

in site/ready.php

/**
 * This hook modifies the default behavior of the Page::path function (and thereby Page::url)
 * this will rewrite the path to children that should not have their own page
 * any automatic links to the page, e.g. in the admin, will direct to the parent page.
 *
 */

wire()->addHookBefore('Page::path', function($event) {
  $page = $event->object;

  if($page->template == 'sometemplate') {
        $event->replace = true;
        $event->return = $page->parent->path;
  }


});

 

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