Run route on uri to get original $page object

I’ve stumbled across this question in other threads. Now I feel it’s time to solve this if possible.

My content uri:s (real paths)


My routed uri:s (fake path)



I want to “convert” my routed uri to my original uri or $page object, something like this.

$uri_or_page_obj1 = route->run('company/microsoft');
$uri_or_page_obj2 = route->run('contact');

echo $uri_or_page_obj1; // computer/item/company/microsoft or object
echo $uri_or_page_obj2; // about/contact or object

Run the route internally to get the original uri or $page. How is it done?

The question is: How does your plugin convert the path to its actual page? Is there any logic you can use or do you use page fields etc.? And why do you want to do this in the first place? I’m just trying to understand what you want to do here.

There are two cases:

1. Page object is not loaded before plugin

It will add this to Github if it’s missing, as you suggested.

2. $page->url() is wrong when routed

When in a template and try this on a routed page it’s not correct:

echo $page->url();

It returns the original uri, not the routed one.

Sure it does, as it’s the page’s URL.

I still don’t really understand what you want to achieve though.

Ok, let’s say I have an archive with pages.

Archive with subpage links

If I have not added any routes this snippet works just fine.

<?php foreach( site()->pages() as $item)  : ?>
    <li><a href="<?php echo $item->url(); ?>"><?php echo $item->title(); ?></a></li>
<?php endforeach; ?>

Don’t work with routes

The above snippet links does not work if the subpages are routed. This is because the $page->url(); return the original url, not the new routed ones.

The routes I use

If you want information about the routes I use, it’s this and similar:

Removed a folder from the url as I’m using folders for panel organisation.

Ah, so it’s the other way around. You don’t need the mapping route -> page but page -> route.

This won’t work out of the box, as Kirby doesn’t know about your custom routes and the custom logic you use for them. To change the links to your routed pages, you can use a page model and set the url method on it to override the URL with a custom one.

1 Like

Can you explain how to do this, please?


Have a look at the docs. A page model can override an existing page method, in this case that would be the page URL:

Suppose you were using a route that omits the blog folder from the url when accessing an article, you would use a page model (/site/models/article.php) like this for the article.php template.

class ArticlePage extends Page {
  public function url() {
    return url() . '/' . $this->uid();

Works! Now i understand the concept.

Thank you very much!

best regards,

EDIT: @texnixe : my comments form doesn’t work anymore now. i’ve checked the apache access log:

With a normal link to the blog article everything works fine (GET): - - [15/Jun/2017:16:03:39 +0200] "GET /kirby/blog/auf-der-suche-nach-dem-glueck-am-arbeitsplatz HTTP/1.1" 200 18567 "http://localhost/kirby/blog/" "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/59.0.3071.86 Safari/537.36"

but if i fill and submit a form (POST)… there is a 404: - - [15/Jun/2017:16:03:46 +0200] "POST /kirby/blog/auf-der-suche-nach-dem-glueck-am-arbeitsplatz HTTP/1.1" 404 31375 "http://localhost/kirby/blog/auf-der-suche-nach-dem-glueck-am-arbeitsplatz" "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/59.0.3071.86 Safari/537.36"

any idea how to fix this?

By default, routes only work with GET requests, try to set the methods in your routes to GET or POST:

1 Like

stupd me. now it works.

thank you! :slight_smile: