Last active
March 14, 2019 09:21
-
-
Save rungta/8a9e3dd48d77be20948751c05f1de0ea to your computer and use it in GitHub Desktop.
Vanity URL routing for Craft CMS
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{# Treat the slug as a template path #} | |
{% set template = include(top_level_slug, ignore_missing = true) %} | |
{# Render the template contents if found, otherwise treat the slug as a username #} | |
{% if template is not empty %} | |
{{- template|raw -}} | |
{% else %} | |
{%- include 'users/_profile' with { username: top_level_slug } -%} | |
{% endif %} | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{# Look for the user #} | |
{% set user = craft.users.username(username).first %} | |
{# Abort if no user was found with that username #} | |
{% if not user %} | |
{% exit 404 %} | |
{% endif %} | |
{# Render the user profile page #} | |
{# ... #} | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Dynamic Site Routes | |
*/ | |
return array( | |
// Existing route declarations | |
// ... | |
// Last rule because it matches all top-level URIs | |
'(?P<top_level_slug>[^/]+)$' => '_vanity_router', | |
); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Updated for Craft 3. See Craft 2 version.