Jump to content
helmut2509

Problem with urlSegment

Recommended Posts

Hi,

I have a template to which I have assigned an Alternate Template filename.

To the respective url I added two urlSegments '../edit/123' . 

In the url segment list I added : "edit" and "new", config->maxUrlSegments = 2.

As long as I only add *one* url segment (edit), it works fine, but when I add two segments (.../edit/123) 

a blank page appears, without error message.

The alternate template file is not even being evoked.

what' s wrong here?

Share this post


Link to post
Share on other sites

can you provide your template code for how you are dealing with the URL segments?

Usually you would check each segment, and if the segment exists, you would sanitize it for further processing, and then take actions based on the validity of the segment.

  • Like 1

Share this post


Link to post
Share on other sites
2 minutes ago, Macrura said:

can you provide your template code for how you are dealing with the URL segments?

Usually you would check each segment, and if the segment exists, you would sanitize it for further processing, and then take actions based on the validity of the segment.

I am just doing this:

if($input->urlSegment(1) == 'edit'){

......

}

...and this works if there is only  *one* segment (edit). If there is a second segment, the script is not being evoked....

Share this post


Link to post
Share on other sites

if you only want to process requests where both segments exits, then you would want to check for both.

this is a simple example:

if($input->urlSegment3) throw new Wire404Exception();

if($input->urlSegment1 == 'edit') {

    if (strlen($input->urlSegment2)) {
    	// process the 2nd segment here
    } else {
        throw new Wire404Exception();
    }

}

if($input->urlSegment1 == 'new') {

	// process request for new

    if (strlen($input->urlSegment2)) {
    	throw new Wire404Exception();
    } 

}

 

  • Like 2

Share this post


Link to post
Share on other sites
6 minutes ago, Macrura said:

if you only want to process requests where both segments exits, then you would want to check for both.

this is a simple example:


if($input->urlSegment3) throw new Wire404Exception();

if($input->urlSegment1 == 'edit') {

    if (strlen($input->urlSegment2)) {
    	// process the 2nd segment here
    } else {
        throw new Wire404Exception();
    }

}

if($input->urlSegment1 == 'new') {

	// process request for new

    if (strlen($input->urlSegment2)) {
    	throw new Wire404Exception();
    } 

}

 

Thanx for your help, but this code will not bring us further.

If there is a second segment, the script is not even being evoked, I just get a blank page....

Share this post


Link to post
Share on other sites

Hello,

I'm a little confused by your description of the problem, but I don't think you have your URL segments set up correctly.  In what follows, I am assuming you  have edited the URL tab of the template for your page to turn on URL segment processing and you have something entered in the "Which URL segments do you want to allow?" field.

I'm guessing that you want to have the first segment be an action - either edit or new - and the second segment to be a numeric id. If that is the case and you want PW to enforce the first segment to be either 'edit' or 'new' then you need to use a regular expression as documented here to match the actual URL you are supplying. More specifically, could you try...

regex:^new/[0-9]+
regex:^edit/[0-9]+

Another approach may be to leave that field totally blank and do all the checking yourself, in which case I think macrura's code should work.

  • Like 3

Share this post


Link to post
Share on other sites

yes, to confirm, my code is designed specifically if you are not restricting within the template settings.

Share this post


Link to post
Share on other sites
7 hours ago, netcarver said:

Hello,

I'm a little confused by your description of the problem, but I don't think you have your URL segments set up correctly.  In what follows, I am assuming you  have edited the URL tab of the template for your page to turn on URL segment processing and you have something entered in the "Which URL segments do you want to allow?" field.

I'm guessing that you want to have the first segment be an action - either edit or new - and the second segment to be a numeric id. If that is the case and you want PW to enforce the first segment to be either 'edit' or 'new' then you need to use a regular expression as documented here to match the actual URL you are supplying. More specifically, could you try...


regex:^new/[0-9]+
regex:^edit/[0-9]+

Another approach may be to leave that field totally blank and do all the checking yourself, in which case I think macrura's code should work.

thanx for your suggestion.

Tomorrow, in the office, I will try this (now I am at home and going to sleep ;-))

Nonetheless I find strange that the script is not even invoked when there are two url segments....

Share this post


Link to post
Share on other sites
9 hours ago, netcarver said:

@helmut2509

Looking at what I wrote again, if you don't require the "new" segment to have an ID, just leave it as "regex:^new".

thanks, now it works like a charm.

it was just a question of using the right regex syntax.....

Share this post


Link to post
Share on other sites

@helmut2509

Ok. I just read the documentation again, and if you don't need an id for the "new" action, you should be able to use...

new
regex:^edit/[0-9]+

Steve

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.


  • Recently Browsing   0 members

    No registered users viewing this page.

  • Similar Content

    • By pwFoo
      How to enable urlSegments runtime instead of persistent in template settings? And where to be loaded before the current page returns a 404 error?
      https://processwire.com/docs/front-end/how-to-use-url-segments/#how-do-you-enable-url-segments
       
    • By iNoize
      Hello, 
      i have an Photography page based on PW. Now is the Problem that the loading time is to long.
      Idea was to split every 20 images in to pages for Ajaxloading. 
      I dont know how generate all 20 images a new URLsegment.
      I just cant  get it.  
      Thanks for advice 
    • By MilenKo
      Hello my friends. Today I started working on my recipe website again and it was the turn to show recipes (pages) that has a specific category assigned in a field.
      In my NowKnow project for categories I used a parent page where inside of it I had the children and everything seemed to be super easy. This time, however, I decided to change the approach so I created a parent page Recipe categories and assigned to it my 'category' template. Inside the parent I added a few categories that I want to be able to select via PageReferrence field 'recipe_category'.
      The parent template would show all the categories represented by a title and an image - that part is done and works fine. 
      Now what I am trying to achieve is to have a few recipes having the 'recipe_category' field equal to Bakery for example, and then when I point to the category URL to get only the recipes that have Breakfast selected in. From what I know the perfect approach to achieve that would be to use $input->urlSegment as to select the name of the category from the URL and then filter the pages adding to selectors: recipe_category=$category.  Following Ryans earlier instructions about the urlSegment and an example found here in the forum, I got this code to fit my fields names:
      <?php if($input->urlSegment1 == 'category' && $input->urlSegment2) { $name = $sanitizer->pageName($input->urlSegment2); $category = $pages->get("template=categories-list, title=$name"); if($category->id) { $q = $pages->find("template=recipes-inner, recipe_category=$category"); } } ?> After adding the code, I enabled the URL segments for both: 'category' and 'category-list' templates. Browsing the URL for the Bakery category:
      http://food.pw/category/bakery/ (the domain name is not a typo, but PW is on my local server) I was supposed to get the $category to get the value of 'bakery'. However instead of that I am getting nothing.
      What am I missing in the big picture as I am sure it is again something silly but I spent almost the whole day trying to figure it out and still got no progress?
      P.S. trying to change the urlSegment number to 3 did not help either
    • By Junaid Farooqui
      Hi guys,
      i was trying to implement SEO URL structure on my another processwire website. SEO team requested us to put .html on every pages. e.g http://www.mydomain.com/products.html and if click any product then it will like http://www.mydomain.com/products/product-one.html so after little bit research i find out it can be done with URLsegment option in template options. i checked on Allow URL Segment, said No to "Should page url end with slash" and said No to "Should URL segments end with a trailing slash? " but after all this setting we try to access the it is showing 404 page. i don't know why.
      Any help will be highly appreciate
      Thanks 
      J
    • By mel47
      Hi,
      I'm quite sure to have read everything, doc and forums, about urlSegment, but I'm still not understanding how to use it. After have set my categories with this excellent post, I'm unable to fix url as I want.
      Page title             |         Template             | actual url                                   |    url I want
      Categ-publi         |         parent_categ      |  categ-publi/article                     | publication/article
         -- Article           |         categories           |
      Publication          |        basic-template    | publication/example1               | publication/article/example1
         -- example1      |        publication          |
       
      For which template I should activated urlSegment and in which template file I should add some code, probably something like this (which didn't work obviously)?
      if($input->urlSegment1 == 'publication') { echo $pages->get('/publication/$categ/')->render(); return; } else if($input->urlSegment1) { // throw a 404 throw new Wire404Exception(); } Thanks so much.
      Melanie
       
×
×
  • Create New...