Jump to content

URL Segments that match a child page name


BrendonKoz
 Share

Recommended Posts

I've run into a bit of a confusing mess. I have a particular section on our site that, for various reasons, I chose to display dynamically by pulling the child page content and displaying it on the parent page. Think of it like the following:

Car Maker (page ID: 2185):

  • General Motors
  • Mazda
  • Ferrari
  • Byd

So General Motors' information is only accessed by viewing the "Car Maker" page, and passing an appropriate url segment value...in this case, the child's page ID. (The children do not have their own PHP, file-based template.)

I wondered though, in this case: Why can't I match either the ID or the name? So I tried it.

It works when querying in Tracy's console. $maker = pages()->get(2185); $maker->child('id|name="mazda"')->title;

It does not work on the "Car Maker" template itself. ProcessWire returns an instance of NullPage.

I'm not entirely sure why Tracy is able to discover the page, while the PHP file template associated to the PW template with URL segments applied cannot...

Thoughts?

I do have a workaround to bypass this issue, though my preference would be to allow this to work instead of using my workaround behavior. 🤷

 

Link to comment
Share on other sites

The docs for the feature say:

  Quote

Lets assume that the path /products/tools/hammer/ resolved to an actual page in our site when accessed at that URL. But lets say that we accessed the URL /products/tools/hammer/photos/ and the /photos/ portion at the end does not resolve to a page. Because "photos" did not resolve to a page, ProcessWire considers this a URL segment for the "hammer" page.

Expand  

So if the URL being loaded is the URL for an actual page (regardless of whether it has a template file or not) then no URL Segment is going to apply.

And if the URL resolves to a "car" page then the template file that's rendered won't actually be "car-maker.php" where your URL Segment code exists. If there's a car template file then it will be "car.php" that's rendered, and if there's no car template file then it will be whatever template file applies to your "404 Not Found" page.

  • Thanks 1
Link to comment
Share on other sites

  On 4/2/2025 at 8:50 PM, Robin S said:

and if there's no car template file then it will be whatever template file applies to your "404 Not Found" page.

Expand  

That's likely the part that I was getting caught up on, and what is happening. I was a little confused when I could query it successfully from a location that wasn't from the template that enabled the URL segment; I originally thought it wouldn't work at all...and the fact that I was able to actually process the request from the PHP template file related to the parent (car-maker), it made me question the docs' in that it prioritized children that existed in the system (as that would seemingly immediately render a 404, and not even show processing from the parent -- i.e.: if I try accessing car-maker/mazda, the car-maker template is being processed, at least initially).

Thanks, @Robin S!

  • Like 1
Link to comment
Share on other sites

I know that I could disable url segments and create an htaccess rule to handle the scenario myself, and in fact I have come up with a workaround that solves the issue that I was having (though without child page name matching).

It wasn't so much that I needed this to work, I was questioning why or if it could work, and if I just misunderstood the documentation. 🙂 Thanks!

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