Jump to content
Valery

urlSegment[n] not working

Recommended Posts

Hi friends,

Being a tiny bit lazy, I copied the $input->urlSegment[n] code from the description of "Allow URL segments" in the template settings.

Up to 3 URL segments are supported after the page URL. These may be any combination of lowercase ASCII letters (a-z), numbers (0-9), dashes and underscores. You can access the URL segments from your template API code by referencing the $input->urlSegment(n) function or $input->urlSegment[n] property (replacing [n] with 1, 2 or 3).

However, that did not work (actually, it displays a character at position n).

I scanned through up my older code and saw that I used $input->urlSegment(n) elsewhere and it worked just fine.

So, the question is probably to Ryan: does the description of "Allow URL segments" need an update? Otherwise, please correct me.

Share this post


Link to post
Share on other sites

As it might be obvious, i'll just ask it anyway, did you put a number from 1 to 3 in your code in the place where the n is? The character n represents a number.

echo $input->urlSegment(1);

Also, you must have URL segments turned on for the related template, and last but not least you must have a urlSegment in your URL.

Lets say your page with a template that has urlSegments turned on is located at:

http://yourdomain.com/foo/

Then if you visit it at the following URL the $input->urlSegment(1) will print "bar"

http://yourdomain.com/foo/bar/

Edit: replaced [1] with (1)

Edited by Raymond Geerts
  • Like 1

Share this post


Link to post
Share on other sites
$input->urlSegment[n] property (replacing [n] with 1, 2 or 3).

it .say replace: [n]

not: n

u have: 

   $input->urlSegment[n]

 nexts u find: [n]

    u replace [n] with: 1 , 2 , or  3

u get

$input->urlSegment1

                                       $input->urlSegment2

$Input->urlSegment3

  • Like 4

Share this post


Link to post
Share on other sites

Ah you're right WillyC, altough this should work fine too

$input->urlSegment(1);

$input->urlSegment(2);

$input->urlSegment(3);

While these below are wrong

$input->urlSegment[1];

$input->urlSegment[2];

$input->urlSegment[3];

  • Like 2

Share this post


Link to post
Share on other sites

Better yet: $input->urlSegment1, $input->urlSegment2, $input->urlSegment3 etc

Share this post


Link to post
Share on other sites
 ... or $input->urlSegment[n] property (replacing [n] with 1, 2 or 3).

$input->urlSegment[n] property  (not array!)

replacing [n] with 1, 2 or 3  (not replacing n with 1, 2 or 3)

$input->urlSegment1, $input->urlSegment2 or $input->urlSegment3

  • Like 3

Share this post


Link to post
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

  • 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 helmut2509
      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?
    • 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
×
×
  • Create New...