Forum

Thread tagged as: Question, Problem

cannot read page argument in addon code

Here is debug code that show up when page loads

Matched route: [lang:lang]/brand/about
Using master page: /templates/pages/about.php
Page arguments:
Array
(
    [0] => /de/brand/about
    [lang] => de
    [1] => de
)

I have an add-on that needs to read [lang] variable, but I can't seem to read it.

I have tried using perch_get('lang'), but does not work. I don't know what else to try.

route pattern in db looks like below

routePattern: [lang:lang]/brand/about
routeRegExp:  ^/(?<lang>[a-z]{2})/brand/about/?$
sawan ruparel

sawan ruparel 0 points

  • 5 years ago

perch_get('lang') is the proper way to retrieve that data from the URL

Rachel Andrew

Rachel Andrew 394 points
Perch Support

You can see that lang is in the page arguments, so you will need to debug your PHP in the usual way.

I was not getting variables via perch_get('lang').

finally, I got it working using below code

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

thought it may help others

Drew McLellan

Drew McLellan 2638 points
Perch Support

I wouldn't advise anyone else to do that. You're running the router twice, using private API methods, and we can't guarantee that the behaviour you're currently getting will persist.

Drew, what would you advice I use. perch_get() is not working for sure.

I found this by debugging the usual PHP way.

Rachel Andrew

Rachel Andrew 394 points
Perch Support

We would advise you use perch_get and figure out why it isn't working in your scenario. We can't support the method you are using.