Jump to content

API, how do I dynamically add a segment url to a template?


Alexey
 Share

Recommended Posts

  • Alexey changed the title to how do I dynamically add a segment url to a template?
  • Alexey changed the title to API, how do I dynamically add a segment url to a template?
if($input->urlSegment1 == 'photos') {
  // display photo gallery
} else if($input->urlSegment1 == 'map') {
  // display map
} else {
  // display main content
}

You mean like this?
Or what exactly do you mean by whitelist ("Without manually adding"?!)

Link to comment
Share on other sites

You can use this to change the template settings’ valid UrlSegments: https://processwire.com/api/ref/template/url-segments/.

Be aware this persists the change immediately, without having to call $template->save()!

You can also use property syntax:

//Add UrlSegments “login” and “logout” to whitelist of current page’s template:
$page->template->urlSegments = array_merge($page->template->urlSegments, ['login', 'logout']);

If you have this requirement, it’s probably worth checking out the relatively new URL hooks: https://processwire.com/blog/posts/pw-3.0.173/

  • Like 1
Link to comment
Share on other sites

5 hours ago, Jan Romero said:

You can use this to change the template settings’ valid UrlSegments: https://processwire.com/api/ref/template/url-segments/.

Be aware this persists the change immediately, without having to call $template->save()!

You can also use property syntax:

//Add UrlSegments “login” and “logout” to whitelist of current page’s template:
$page->template->urlSegments = array_merge($page->template->urlSegments, ['login', 'logout']);

If you have this requirement, it’s probably worth checking out the relatively new URL hooks: https://processwire.com/blog/posts/pw-3.0.173/

That didn't work, for me it worked like this:

$template = $templates->get('home');
$template->urlSegments = array_merge($template->urlSegments, ['new_city']);
$template->save();
 
ChatGPT helped 😀
Link to comment
Share on other sites

Not sure I understand the question exactly, but the general way to handle URL segments is in the template file. The template setting is just for if you only have one or two fixed segments or have some reason not to use the template file.

So in your template file you just throw a 404 if the segment doesn't match your allowed segments, which could be defined dynamically. E.g.

// Throw 404 if segment1 isn't in the array of allowed segments (which you could define dynamically)
if($input->urlSegment1 && !in_array($input->urlSegment1, $my_allowed_segments)) throw new Wire404Exception();

// Throw 404 if there is more than one segment when there shouldn't be
if($input->urlSegment2) throw new Wire404Exception();

// Now generate output according to segment, no segment, etc...

Also see the docs here: https://processwire.com/docs/front-end/how-to-use-url-segments/#best-practices

 

Link to comment
Share on other sites

57 minutes ago, Robin S said:

Not sure I understand the question exactly, but the general way to handle URL segments is in the template file. The template setting is just for if you only have one or two fixed segments or have some reason not to use the template file.

So in your template file you just throw a 404 if the segment doesn't match your allowed segments, which could be defined dynamically. E.g.

// Throw 404 if segment1 isn't in the array of allowed segments (which you could define dynamically)
if($input->urlSegment1 && !in_array($input->urlSegment1, $my_allowed_segments)) throw new Wire404Exception();

// Throw 404 if there is more than one segment when there shouldn't be
if($input->urlSegment2) throw new Wire404Exception();

// Now generate output according to segment, no segment, etc...

Also see the docs here: https://processwire.com/docs/front-end/how-to-use-url-segments/#best-practices

 

I understand, for example, that today there is a pharmacy in this city, and tomorrow it stopped working with us, and if this pharmacy is the only one in the selected region, then it is removed and, of course, the city must be removed, everything happens dynamically. I just switch from yii2 to processwire, everything happens dynamically there without manual intervention.

Link to comment
Share on other sites

11 hours ago, Jan Romero said:

Be aware this persists the change immediately, without having to call $template->save()!

Okay, so, I didn’t think this should happen and indeed it usually doesn’t, but it definitely happens… sometimes??? This is the code I used to test it initially, just at the top of my home template. I visited the page, checked the UrlSegments in the admin and there they were:

header('content-type: text/plain');
$page->template->urlSegments = array_merge($page->template->urlSegments, ['hello', 'world']);
die();

I tried looking into core/Template.php but nothing seems suspicious… No idea what’s going on but I can reproduce the behaviour on two separate installations, one running 3.0.208 dev and one 3.0.191 dev. I’m not going to investigate this further and blame it on a module or a dumb hook I have set up, but I’m weirded out.

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