0

I am trying to have a dynamic uri segment for a single. I have this uri in the CP settings:

offers/listing/{slug}

Where {slug} is the entryId. For example, offers/listing/1234

I have a template at templates/listing/make-offer.twig

For the template path i have: listing/make-offer

If I navigate to site.com/offers/listing/1234 I get a 404.

However, if I make a custom route:

URI looks like: offers/listing/(slug)
Loads template: listing/make-offer

Everything works. The downside is I would like to control the content on the top of the page through the CP.

I realize, this is something more of a channel (which I still get a 404), but this page will never go anywhere. It's a content page at the top with a contact form at the bottom.

Thank you for any suggestions!

2

Assuming that a Single is the right thing to use here (and I'm not sold on that but I trust your judment) the following is what I would do to accomplish what you want.

// config/routes.php
'offers/listing/(?P<entryId>\d)' => 'listing/make-offer'

// templates/listing/make-offer.twig
{% set entry = entry is defined ? entry : craft.entries.id(entryId) %}

Explanation

First, we create an Advanced Route to make the (single) entryId available to the template that will be loaded when the route matches.

Then, in the template, we set the entry variable to either the entry that Craft makes available to the template via its own routing or we fetch one explicitly by using the entryId passed to the template from the custom route. Our route may take precedence over default routing, so the entry is defined check may be removed and rely instead on craft.entries.id(entryId)

End Result

You should be able to navigate to /offers/listing/1234, which will load the templates/listing/make-offer.twig template with the entryId set to 1234.

Hope this helps, feel free to follow up if something isn't clear.

2
  • Thank you! This makes perfect sense - I'm still getting a 404 though. Is it possible Craft is still reading the uri segment set in the section? It's a required field, so I'm not sure how to override it.
    – Damon
    Apr 1 '17 at 17:47
  • May need to update the single to use a fixed URI rather than slug?! Apr 1 '17 at 18:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.