Forum
Collection detail pages and pretty URLs
Hi, folks! I'm having some difficulty figuring out how to make a Collection-populated detail page have a pretty url.
Here's the structure of my site, a commercial real-estate project:
Home
- Locality A
-- Property 1
-- Property 2
-- Property 3
- Locality B
-- Property 4
-- Property 5
-- Property 6
[etc]
The Home page needs to host links to each Locality page. Each Locality page displays links to the various Properties in that area. I thought the best way to organize this was to make Collections of both the Localities and the Properties, since they are related.
While it was fairly easy to create Localities and display them as a list on the home page, getting an individual Locality page was hard to figure out. I wanted to use a two-page list-detail approach as per https://docs.grabaperch.com/perch/content/functions/two-page-list-and-detail/
I ended up creating a locality page in Perch admin, using a file at templates > pages > locality.php, with the following php embedded in it:
<?php
perch_collection('Localities', [
'template' => 'localities.html',
'filter' => 'locality_slug',
'match' => 'eq',
'value' => perch_get('s'),
'count' => 1,
]);
?>
Then in my localities-list.html template, I change the link hrefs like so:
<a href="/locality?s=<perch:content id="locality_slug" type="slug" />">
<img src="<perch:content id="locality_thumbnail" type="image" />" alt="<perch:content id="locality_name" type="text" />">
</a>
This works, populating locality.php with the given Collection, but I want cleaner URLs. Instead of:
https://localhost/locality?s=locality-a
I want:
https://localhost/locality-a
I tried playing with mod_rewrite but could not get it to work.
So in the Page Options for the Locality page in admin, I tried putting [slug]
into the URL pattern. This sort-of works, allowing me to address the page with https://localhost/locality-a
, but I realize it's not going to be able to match on the URL because the custom route strips out the query variable ("s") that's necessary to match a collection item.
Is there a better way to clean up my URLs the way I want?
Joel, Is Perch Runway out of Budget? As this kind of thing is really easy in Perch Runway.
EDIT: I was not paying attention today... this is obviously Perch Runway.
Ok, so your route would be
[slug:s]
and you would access it usingperch_get('s')
https://docs.grabaperch.com/runway/routing/named-segments/
No problem!
Here's hoping someone can tell me how easy it is! It's our first Runway project, and I am finding some of it easy and some of it conceptually challenging.
Is this not working?
In my test after setting the route to
[slug:s]
then using the urlhttps://myFancyUrl.com/this-is-slug
I get this output in debug:Bingo! Thanks for the tip!
Having puzzled over that named-segments doc page, I'd earlier tried inputting `[slug:[locality_slug]] in there, but that didn't work.
But
[slug:s]
works like a charm.Thanks, Robert!