Jump to content

Finding a page anywhere in the tree with urlSegment


lenoir
 Share

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.

Link to comment
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=

Link to comment
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
Link to comment
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
Link to comment
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?

Link to comment
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.

Link to comment
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…

Link to comment
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.

Link to comment
Share on other sites

  • 1 month later...

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.

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

×
×
  • Create New...