Forum

Thread tagged as: Runway

Analyze Routes in runway

In Runway, it is possible to define multiple routes per page. That is awesome. How do I work with those routes after a page is called?

What I want to do:

  1. Page has 3 routes: lorem1, lorem2 and lorem3
  2. When the page has been found, find out through which route token we have come in. Say it's "lorem2"
  3. Get an array of all defined routes in the order they were entered in the backend
  4. Then I would like to know which position the chosen token had in that list.

That's it. Drew, you might guess what I'm trying to achieve here

Urs Bräm

Urs Bräm 1 points

  • 5 years ago
Drew McLellan

Drew McLellan 2638 points
Perch Support

If you turn on debug you can see how the route has been matched. The components are available through perch_get().

I get the matched route

Matched route: leteste
Page arguments:
Array
(
    [0] => /leteste
)

but not a list of all routes defined in the backend.

Later on the same page, I planned to filter content depending on the position of the selected route in the route list.

The goal of this is of course to edit one page with fields in multiple languages.

Drew McLellan

Drew McLellan 2638 points
Perch Support

It just doesn't work like that. You don't get a list of routes that could've matched but didn't.

Is there a way to analyze a route and get a pattern from it? Maybe match it to a <perch:pages> field?

Drew McLellan

Drew McLellan 2638 points
Perch Support

What are you trying to do?

I was trying to find out if a "single tree" structure with perch Pages is possible with existing on-board tools.

Homepage (all on one page, 3 langs)
+-Subpage (all on one page, 3 langs)
+-Subpage (all on one page, 3 langs)

So I wanted to detect from the URL/route which language has been preselected.

Wich, having given it a little more thought, is not even necessary: Just add a language marker like /en/ to the URL and go from there.

But how do I handle that incoming route then? Say it's /en/aboutus and /de/ueberuns Hmmm, I could strip the first bit in .htaccess and store it in $_GET['lang'], and then send the rest of the URL This might also work with domains as indicators for the language.

# Perch Runway
RewriteEngine On
RewriteRule ^/de/(.+)$ $1 [L,NC]
RewriteRule ^/fr/(.+)$ $1 [L,NC]
RewriteRule ^/en/(.+)$ $1 [L,NC]
RewriteCond %{REQUEST_URI} !^/perch 
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule .* /perch/core/runway/start.php [L] 

What do you think about prepping a runway URL in that way?

Drew McLellan

Drew McLellan 2638 points
Perch Support

Would it help if there was some way to configure a routing prefix globally?

If I can bring it into each navigation template via PerchSystem::set_var() and if it's safe to do some rewriting like the above before passing it to runway: I don't think that would be even necessary.

Drew McLellan

Drew McLellan 2638 points
Perch Support

If it works then it's fine to do.

It doesn't. Probably due to my .htaccess skills. It's too early for me to ask you for the routing prefix, though, as I'm only doing this proof of concept style.

Just an idea: if in start.php instead of

$RoutedPage = $Router->get_route($_SERVER['REQUEST_URI']);

you'd replace that by

$RoutedPage = $Router->get_route(PERCH_REQUEST_URI);

And then in config.php

define('PERCH_REQUEST_URI', $_SERVER['REQUEST_URI']);

I (ehm, integrators...) could modify the value for PERCH_REQUEST_URI dynamically (e.g. with a simple str_replace).

Drew McLellan

Drew McLellan 2638 points
Perch Support

Ok. Let me know how you get on.

Hmm.. getting on not too bad.

If the language is based on the domain, it should work, start.php uses the REQUEST_URI.

RewriteCond %{HTTP_HOST} ^germandomain.ch$ 
RewriteRule ^(.*)$  $1?lang=de
RewriteCond %{HTTP_HOST} ^frenchdomain.ch$ 
RewriteRule ^(.*)$  $1?lang=fr

I will be using this approach and will post the working .htaccess later.

Albeit, with the prefix it's not so easy, as a Redirect (R) flag is necessary to alter the REQUEST_URI - which of course, results in a redirect, which we don't want.

RewriteRule ^(de|fr|it)/(.*)$  index.php?lang=$1 
RewriteRule ^(.*)$  $1?lang=de

So yes, maybe a global routing prefix would be a fine idea. Or, see my last post, allow developers to determine from where $Router->get_route gets its information (without hacking start.php)! That would be probably be the simpler way.

Happy Holidays!