Run route on uri to get original $page object


#1

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)

computer/item/company/microsoft
about/contact

My routed uri:s (fake path)

company/microsoft
contact

Question

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?


Functions unable to load the $page object
#2

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.


#3

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.

http://forum.getkirby.com/t/functions-unable-to-load-the-page-object/1081

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.


#4

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

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


#5

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.

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

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:

http://getkirby.com/docs/advanced/routing#omitting-the-blog-folder-in-urls

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


Changing/customizing the URL of subpages
#6

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.


Routes and $page->url() doesn't match
#7

Can you explain how to do this, please?

thx,
Svnt


#8

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.

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

#9

Works! Now i understand the concept.

Thank you very much!

best regards,
Svnt

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):

127.0.0.1 - - [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:

127.0.0.1 - - [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?


#10

By default, routes only work with GET requests, try to set the methods in your routes to GET or POST: https://getkirby.com/docs/developer-guide/advanced/routing/#methods


#11

stupd me. now it works.

thank you! :slight_smile:

Svnt