3

For a project I have, I am unable to use the standard Craft routing for a channel. For example,

/events/{slug}  <- this is how Craft wants to do it
/{slug}         <- this is how the client wants it done

I have tried to do this using routes, and can easily route to the correct template, however the entry is not defined.

Is there any easy way to not have to use Craft's routing that requires the events portion of the url? And have the entry variable available in the template?

Added to this is the fact I need to do URLs like

/{slug}/blog/{article-slug}
/{slug}/faqs/{question-slug}
etc etc

The homepage is a static template, but as there is no content to exist under /events, and being that events is the root of all other content (which is filtered by the event slug) I can't see a way so far to not have to use the full path the 'Craft' way.

Bonus Question:

Is there any way to prevent /events/{slug} from responding to requests also? Is it just using the exit 404 tag in templating?

5

I'm not sure I understand your question, but to try:

There actually isn't a "standard Craft routing", for channel Sections or anything else.

In your channel's Section settings (available via Settings -> Sections -> [your event section]), there's a field called Entry URL format. Simply add {slug} to that field (and nothing else), and you should be good to go. You can even use the same URL format for different sections.

In regards to your "bonus question", if you set the Entry URL format to simply {slug} for the events section, Craft will probably throw 404 for any /events/{slug} URLs, out of the box.

As for dynamic routes, you're correct that Craft won't populate your template with the entry variable – it will however, populate the template with variables matching the tokens that you add to your route. So, if you e.g. create a dynamic route that uses the <slug> token, you can pull the entry in the template like this:

{# At the top of your template: #}

{% set entry = craft.entries.section('events').slug(slug).first() %}
{% if not entry %}
   {% exit 404 %}
{% endif %}

{# ...the rest of your template #}

Beyond the above – if you're still unsure how to set things up properly, I'd suggest taking a look at the official docs on routing, which explains in detail how Craft evaluates requests and what your different routing options are. Particularly important if you figure that you do need to do some custom routing beyond what the URL format setting can do.

|improve this answer|||||

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.