Jump to content
lenoir

Finding a page anywhere in the tree with urlSegment

Recommended Posts

Hi there,

Simple question needs simple answer… I'm still a newby and trying to get my head round pw.

I'm trying to access (and eventually get the path) to a particular page somewhere else in the tree, without knowing the complete path, only knowing the name of the page.

I'm getting the name of the page with urlSegment. Code looks like this so far:

$myPage=$input->urlSegment1;
$pathToPage=$pages->find($myPage)->path;

Played around with it, browsed the forum and failed embarassingly.

Any suggestions?

Thanks.

Share this post


Link to post
Share on other sites

Try something like find("name=$mypage") in your code above since you don't have the full path available to you.

I'm assuming you're wanting to match against the sanitised page name (ie. the URL version of the page title) - if not its title= and not name=

Share this post


Link to post
Share on other sites

One thing to remember: there can be multiple pages with same names (but with different parent). But here is how you do it (this returns the first page it finds):

$myPage = $input->urlSegment1;

// Remember to sanitize!
$myPage = $sanitizer->pageName($myPage);

// $pages->get() returns single page while $pages->find() returns PageArray
$p = $pages->get("name=$myPage");

// And finally, we want the url
echo $p->url;
  • Like 2

Share this post


Link to post
Share on other sites

Thanks to both of you! I wasn't aware of the sanitizing process…

Works like a charm!

I'm using the page name because it appears in the URL and i want to keep them clean.

  • Like 1

Share this post


Link to post
Share on other sites

Technically the pageName sanitizer isn't actually necessary here (as PW wouldn't even get the request if the pagename wasn't in the right format), but it is a best practice to sanitize anything you are planning on putting in a selector. So I would do it the same way as apeisa and sanitize with pageName, even if it's not technically required.

  • Like 2

Share this post


Link to post
Share on other sites
One thing to remember: there can be multiple pages with same names (but with different parent).

…and this problem just occured. I could solve it by adapting the conflicting page's name, but on the long run it's definately not going to be the best bet. Should I avoid using their name to get pages?

Share this post


Link to post
Share on other sites

You could use ID_name or name_ID.

  • Like 1

Share this post


Link to post
Share on other sites

Hi, I'm posting another question here, since it's related.

I have a URL like this: …/company/team

On this page, i have all the team members linked

<a href='#/{$child->name}'>{$child->title}</a>

However, i can't get the urlsegment to know the name of the team member.

$member=$input->urlSegment2;

urlsegment1 doesn't work either.

Won't UrlSegments work with dashes?

Any suggestion?

thanks for your help!

EDIT: Url Segments are allowed.

Share this post


Link to post
Share on other sites

Ok, solved the issue. Apparently urlSegments don't work with "/#/". I replaced this URL:

…/company/team/#/member

by:

…/company/team/member/member-name

This not only works, but actually makes more sense…

Share this post


Link to post
Share on other sites

A hash is usually used in a URL to jump to a HTML anchor further down a page (like <a name="bob"></a> halfway down a page called "tim.html" could be called via yoursite.com/tim.html#bob and that page would load and automatically jump down to that anchor).

I'm not sure an anchor would work any other way due to this.

Share this post


Link to post
Share on other sites

Hi there.

One month later, one project later: same issue (almost).

Processwire's growing on me quickly, but there are a couple of things that kind of drive me nuts. And one of these is the whole selector / urlsegment thing. For some reason I struggle a lot with those and can't find answers to my questions in the tutorial or on the web.

Therefore, my issue below. The following code doesn't work.


$segment=$input->urlSegment1;
$segment=$sanitizer->pageName($segment);

foreach($pages->find('wh_mitarbeiter_kategorien=/portrait/mitarbeiter/kategorien/{$segment}') as $child){
//magic here.
}

Now if i replace the {$segment} bit by 'a-bis-z' (which is the category i'm looking for), it works perfectly.

Any idea? Or do you need more information?

Thanks for the help.

Share this post


Link to post
Share on other sites
$pages->find('wh_mitarbeiter_kategorien=/portrait/mitarbeiter/kategorien/{$segment}')

'strings' won't get parsed in php

try "strings"

Share this post


Link to post
Share on other sites

hmm. Tried it, doesn't solve the problem though.

hmm N°2. Might have actually solved the problem. I had to reset Safari and now it works… Go figure.

Thanks Soma.

  • Like 1

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 alanxptm
      Hi
      I want to list the latest articles on the site, the articles have multiple templates using the same template php archive.
      I have tried with:
      $pages->find('template=lvl04-post, limit=5'); but didn't work. Also, I have read about $template->filename, but don't understando how to use it with find()
      Articles and parent categories have their own single templates because of role permissions, so I can't just do a normal 'template=name'
      This is the full code:
      <?php $posts = $pages->find('template=phpfilename?, limit=5'); foreach($posts as $post){ ?> <a href="<?php echo $post->url; ?>"><?php echo $post->title; ?></a><br /> <?php echo date("d/m/Y", $post->published); ?><br/> <?php echo $post->resume; ?><br/> <?php } ?> Any help is appreciated 😮
    • 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?
×
×
  • Create New...