I'm using following code to route urls to specific template

'<restaurantSlug:{slug}>-nutrition' =>  ['template' => '_nutrition/restaurant'],

is there a way to limit slug results to specific channel otherwise I can add anything before -nutrition and it's getting routed as well. Which is not that clean and also don't allow me to use redirects (retour plugin) since the page don't actually throw 404 error.

Or any other approach that I've missed?


1 Answer 1


The app won't know about your Channels when it's checking static routes, without some trickery, or some very verbose auto-generated routes—at which point it seems like you might as well just use the built-in Element routing functionality. Can I ask why your Nutrition section can't just have a URI like {slug}-nutrition in the first place?

You can, however, perform this check once the request arrives at the template—something like this:

{# Set the additional `section` constraint when fetching the Entry! %}
{% set customRouteEntry = craft.entries({
  section: 'nutrition',
  slug: restaurantSlug
}).one() %}

{% if not customRouteEntry %}
  {% exit 404 %}
{% endif %}

<h1>{{ customRouteEntry.title }}</h1>
  • Great. Thanks. Well the restaurant' nutrition entries doesn't exist and generated on the fly by using entries fields. Complicated setup.
    – Mark H.
    Mar 29, 2020 at 0:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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