Forum

Thread tagged as: Question

Change value of variable using URL

Hi,

I'm using this setup to populate $navgroup with a value from the URL.

$page_url = perch_page_url(['include-domain' => false,], true); // Output the full URL of the current page
$dir_name = dirname($page_url); // Returns a parent directory's path  
$navgroup = str_replace('/','', $dir_name); // Replace all occurrences of the search string with the replacement string  
print $navgroup; 

It only half works, here are some examples:

Example 1: site.com/de/demo/

$navgroup has a value of de when I visit the demo page. This is what I want to happen.


Example 2: site.com/de/

$navgroup has a value of / when I visit the de page

I need it to have a value of de


Example 3: site.com/de/demo/test

$navgroup has a value of demo when I visit the test page

I need it to have a value of de


Wherever the user is on the de branch, I'd like $navgroup to output de

What's the best way to achieve this?


I'm trying to get this to work so I can use $navgroup to change the main navigation on a multi-language website.

Using something like this:

perch_pages_navigation(['navgroup'=> $navgroup]); // output navgroup based on value of $navgroup
Stephen Meehan

Stephen Meehan 4 points

  • 3 years ago
Drew McLellan

Drew McLellan 2638 points
Perch Support

So is the value you want always the first url segment?

Hi Drew,

Yes please.

Hussein Al Hammad

Hussein Al Hammad 105 points
Registered Developer

Hello Stephen,

Assuming you only get the URL this way:

$page_url = perch_page_url(['include-domain' => false,], true);

So the value of $page_url doesn't include https://, you could use something like this:

$url_parts = explode("/", $page_url);
$lang = $url_parts[1];

Great stuff, thanks Hussein.

I'm working on a multi-language website (using the branching technique), and was looking for a way to use part of the url as a variable

Putting it all together, here's what I'm using change the navgroup based on the current language branch. I've also used set_var so I can use $nav_group in my html templates.

$page_url = perch_page_url(['include-domain' => false,], true); // Output the URL of the current page, minus http
$url_parts = explode("/", $page_url); // Split a string by a string
$nav_group = $url_parts[1]; // Output the first part of string 
PerchSystem::set_var('lang', $nav_group); // Set 'lang' for use in html templates
perch_pages_navigation(['navgroup'=> $nav_group]); // Output navgroup based on value of $nav_group
// print $nav_group; // Test output value of $nav_group 
Drew McLellan

Drew McLellan 2638 points
Perch Support

In Runway you can also grab the segment from the route using perch_get().

Hi Drew,

I'm curious, how would I grab the segment from the route using perch_get()?

Drew McLellan

Drew McLellan 2638 points
Perch Support

It's usually perch_get(0), or you can name it in your route. The routing values are always displayed in the debug for a routed page.

Ah, yes, very neat. Thanks.