<p>The app won't know about your Channels when it's checking static routes, without some trickery, or some <em>very</em> 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 <em>Nutrition</em> section can't just have a URI like <code>{slug}-nutrition</code> in the first place?</p>

<p>You <em>can</em>, however, perform this check once the request arrives at the template—something like this:</p>

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

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

&lt;h1&gt;{{ customRouteEntry.title }}&lt;/h1&gt;
</code></pre>

CC BY-SA 4.0
2020-03-29T00:03:24.427Z
Great. Thanks. Well the restaurant' nutrition entries doesn't exist and generated on the fly by using entries fields. Complicated setup.
Mark H.
CC BY-SA 4.0
2020-03-28T22:38:46.250Z
2020-03-28T22:38:46.250Z
August Miller
<p>I'm using following code to route urls to specific template</p>

<pre><code>'&lt;restaurantSlug:{slug}&gt;-nutrition' =&gt;  ['template' =&gt; '_nutrition/restaurant'],
</code></pre>

<p>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.</p>

<p>Or any other approach that I've missed?</p>

<p>Thanks.</p>

<routes>
Filtering routes
# Filtering routes

(Asked by: username_0 on 2020-03-28)

I'm using following code to route urls to specific template
'-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?
Thanks.

**Tags:** routes


---

## 1 answers

### Answer by username_1 on 2020-03-28. Score: 2 | Accepted answer

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 %}

{{ customRouteEntry.title }}

> **Comments:**
> **username_0**: Great. Thanks. Well the restaurant' nutrition entries doesn't exist and generated on the fly by using entries fields. Complicated setup.  

---

