Is it possible to have one page with variable child

I’m trying to setup a webshop that might expand to multilingual down the line.
So instead of having pages for every product and then having to duplicate them to every context/language, I would much rather have a non-MODX database table — let’s call it “products” for now — that stores the basic product information, like a product number, name (that won’t change across the different contexts) and price.

Would it be possible to create one page — i.e. “shop.com/product” — within MODX, where you still get a friendly URL to get the product? So for “My first product” you could go to “shop.com/product/my-first-product” rather than “shop.com/product?p=my-first-product” or something?

I know the standard .htaccess file take the whole path into a q variable. But I think you would kind of want to process that first in between somewhere.

Or would a separate context with site_url “shop.com/product/” be the better way to go, where there’s just one page there and you get a new q variable value, that has all the information I’d need.

I understand the easy answer is to simply do individual pages. But if there’s a way to save doing duplicate work down the line (and potentially miss out on a few changes), that would be the preferred way to go.

Yes, this is possible.

You could use the extra CustomRequest or write your own custom plugin that runs on the event OnPageNotFound.

Thank you so much!

I never knew this one existed. Then again: it’s impossible to know everything that’s out there. And I don’t think I would have ever come to some kind of phrasing of a search query to get it to show up in Google either.

I just gave it a try and it seems to do exactly what I need. I made the global “/product” page and visited “/product/test” and did manage to get the the “test” string.

From there on out, I’ll be able to get the rest of it working as well.

Once again: thank you!

1 Like

Hi,
I would like to understand in detail how to do it.
I have a /team.html page that reacts based on parameters sent
With
mysite .com/team.html ?idTeam=1
the snippet inside team, go to DB and grab the team with id = 1 and relative data, such as name
If in this table i put a column with “dbalias”, I would like the address URL to be
mysite .com/dbalias.html and not mysite .com/team.html ?idTeam=1

how can I do it with CustomRequest and, above all, with my custom plugin?

thanks in advance

Let’s say you make a request to your site with the URL mysite.com/team/member_alias.html. When MODX can’t find a resource with this URL, the event OnPageNotFound is invoked.

You can create a custom plugin that runs on this event.
You then check if the requested URL (usually the request parameter q) has a certain format. (For example something like team\/[a-z_]+\.html.)
If it’s a match, you forward the request to the team.html page using the MODX function $modx->sendForward() and save the alias to the PHP superglobal $_GET or $_REQUEST (E.g. $_GET['team_alias'] = $alias).
On the team.html page you then use a normal snippet to display the correct team member info based on the value of $_GET['team_alias']).

This is also basically how the extra CustomRequest works.

1 Like

This topic was automatically closed 2 days after discussion ended and a solution was marked. New replies are no longer allowed. You can open a new topic by clicking the link icon below the original post or solution and selecting “+ New Topic”.