Alexey Posted March 27, 2023 Share Posted March 27, 2023 API, how do I dynamically add a segment url to a template? Link to comment Share on other sites More sharing options...
zoeck Posted March 28, 2023 Share Posted March 28, 2023 Have you looked at the instructions for URL Segments? https://processwire.com/docs/front-end/how-to-use-url-segments/ Link to comment Share on other sites More sharing options...
Alexey Posted March 28, 2023 Author Share Posted March 28, 2023 7 minutes ago, zoeck said: Have you looked at the instructions for URL Segments? https://processwire.com/docs/front-end/how-to-use-url-segments/ Yes, how do i add url segment to whitelist? Without manually adding... Link to comment Share on other sites More sharing options...
zoeck Posted March 28, 2023 Share Posted March 28, 2023 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 More sharing options...
Jan Romero Posted March 28, 2023 Share Posted March 28, 2023 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/ 1 Link to comment Share on other sites More sharing options...
Alexey Posted March 28, 2023 Author Share Posted March 28, 2023 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 More sharing options...
Robin S Posted March 28, 2023 Share Posted March 28, 2023 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 More sharing options...
Alexey Posted March 28, 2023 Author Share Posted March 28, 2023 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 More sharing options...
Jan Romero Posted March 28, 2023 Share Posted March 28, 2023 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 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