Page 1 of 1

How to Get Route Values from URL (v2021)

Posted: Fri Sep 25, 2020 4:23 pm
by mobhar
PHPMaker v2021 requires URL Rewrite to display the SEO-Friendly URL.

Now you may parse its URL to get the current page name and the primary key value(s) so easy, by using Route(n) function; which n is the index number of route (started from 0 as the current page name, followed by the index of its primary key value(s)).

This is very useful if you want to parse the URL for the Edit or View page; which displays one single record, and the current table has more than one primary key fields.

Let's say, from the "Edit" page of "orderdetails" table of demo project, we have the following URL:

orderdetailsedit/10248/41

You may simply get each of that Route from that URL, by simply put this following code in "Row_Rendered" server event:

$this->setMessage("Route 0: " . Route(0));
$this->setMessage("\nRoute 1: " . Route(1));
$this->setMessage("\nRoute 2: " . Route(2));

That code will result:

Route 0: orderdetailsedit
Route 1: 10248
Route 2: 41

The same code for this URL: orderdetailsview/10248/41 will result:

Route 0: orderdetailsview
Route 1: 10248
Route 2: 41

As we can see, the "Route 1" is the value from "OrderID" field, and the "Route 2" is the value from "ProductID" field in "orderdetails" table. The order of these values depends on how you put the order of those fields from "Fields" setup of your project.

Re: How to Get Route Values from URL (v2021)

Posted: Fri Oct 02, 2020 2:38 am
by FedeLopez
Also you may use Route("key_value");

In your example should be:
Route("OrderID");
Route("ProductID");

To know wich "key_values" are available, you may do var_dump(Route());

Re: How to Get Route Values from URL (v2021)

Posted: Fri Oct 02, 2020 8:47 am
by mobhar
Thanks for the info, FedeLopez. It is very useful, too.