Skip to content

Custom Taxonomy Routes #2403

Closed
Closed

Description

@warkior

Bug Description

We seem to have lost the ability to use custom taxonomy routes.

I have a taxonomy named Magazine Topic. It seems to also suffer from issue #2273.

On Statamic v2 we had defined a custom route for the taxonomy to use /magazine/topic/{slug}. I don't seem to be able to reproduce it on Statamic v3.

Statamic version: 3.0.7

PHP version: 7.3

Activity

  1. mikemartin commented on Sep 15, 2020

    @mikemartin
    Contributor

    Hey @warkior
    I agree that there should be a way to customize the taxonomy routes but have you tried naming your taxonomy topic and adding that taxonomy to your magazine collection settings? Then the taxonomy url /magazine/topic/{slug} should resolve.

  2. JonKaric commented on Nov 15, 2020

    @JonKaric
    Contributor

    +1 for this. Working with taxonomy routes and having them Just Work™ is a little harder in v3. It would be nice to be able to define a custom route and still have access to the data, like on the default routes, available there too.

  3. FrankPeters commented on Nov 25, 2020

    @FrankPeters

    +1 for this as well. We also heavily relied on custom routing functionality. And we do not use a similar structure as @warkior as we use taxonomies both as content as well as filtering for blog items for instance.

    I'm going to check if we can use this workaround, but I am afraid it will not be compatible.

  4. FrankPeters commented on Nov 26, 2020

    @FrankPeters

    I have come to the conclusion that we cannot implement the workaround as suggested. So we are now forced to migrate the taxonomy to a collection instead, as it allows us to use a custom routing scheme.

    This is really disappointing as this breaking change is not even documented on the https://statamic.dev/upgrade-guide#breaking-changes-core page.

  5. JonKaric commented on Nov 26, 2020

    @JonKaric
    Contributor

    @FrankPeters You can still define custom routes in your routes.php file. You can then use {taxonomy_term} in your templates to get the term. It's just a bit more limited than the automatic routes that Statamic provides.

    Route::statamic('reviews/category/{taxonomy_term}', 'my_view'); 
    

    I do wish it was possible to 'attach' a taxonomy to a route like the one above, though.

  6. FrankPeters commented on Nov 26, 2020

    @FrankPeters

    @JonKaric Thanks! It seems to have worked. This will save a ton of work. Still have to solve some issues, but they appear minor now compared to converting to collections ;-)

  7. duncanmcclean commented on Nov 26, 2020

    @duncanmcclean
    Member

    @FrankPeters Oops, pretty sure it wasn't intentionally left off the list of breaking changes. Thanks for pointing that out though. I've PR'd it into the docs statamic/docs#319.

  8. github-actions commented on Jan 26, 2021

    @github-actions

    This issue has not had recent activity and has been marked as stale — by me, a robot. Simply reply to keep it open and send me away. If you do nothing, I will close it in a week. I have no feelings, so whatever you do is fine by me.

  9. JonKaric commented on Feb 2, 2021

    @JonKaric
    Contributor

    Re-opening this

  10. 6 remaining items

  11. jasonvarga commented on Apr 14, 2021

    @jasonvarga
    Member

    Just pointing that out since some comments sound like they didn't know that was possible.

  12. 1stevengrant commented on Apr 14, 2021

    @1stevengrant
    Contributor

    sure - I've not had any issues on new builds but definitely a quirk on a migration

  13. stephenmeehanuk commented on Apr 14, 2021

    @stephenmeehanuk

    and resources/views/blog/tags/show.antlers.html will make /blog/tags/mytag will be available.

    I guess I was wondering if it's possible to drop the taxonomy name from the URL?

    When viewing /blog/tags/mytag could /blog/mytag be used instead?

  14. jasonvarga commented on Apr 14, 2021

    @jasonvarga
    Member

    @stephenmeehanuk where are your blog posts located? /blog/{slug}? wouldn't that conflict with /blog/{tag}?

  15. stephenmeehanuk commented on Apr 14, 2021

    @stephenmeehanuk

    @jasonvarga I guess there would be a conflict...

    I'm curious how this bit works though.

    Using the example from the docs, on /blog/tags/retrowave all the 'retrowave' entries would be listed. But the url to each entry would be something like /blog/retrowave/entry-post-url-here.

    Based on the need for the taxonomy slug to be present in the URL for the collection term page, I would expect it to be used in the entry url - but it's not?

  16. jasonvarga commented on Apr 14, 2021

    @jasonvarga
    Member

    But the url to each entry would be something like /blog/retrowave/entry-post-url-here.

    Where are you getting that from? The URL of the entry is defined in the route in the collection. If it's route: 'blog/{slug} then the url would be /blog/entry-post-url-here. The entry urls aren't affected by taxonomies.

  17. stephenmeehanuk commented on Apr 16, 2021

    @stephenmeehanuk

    Where are you getting that from?

    Apologies, I had the wrong end of the stick! I get taxonomy URL and collection URL are two different things.


    Are there (any near future) plans to bring back custom taxonomy routes as mentioned at the top of this post? Or fixing the issue @1stevengrant mentions? Seems like the docs say custom taxonomy routes are possible, but it's not working?

    Currently I need to use this structure when using a taxonomy with a collection collection-name/taxonomy/term, it'd be nice to be able to use collection-name/term instead.

    I think in most cases I could live with the default structure collection-name/taxonomy/term, but for a particular website I'm working on I need three taxonomies.

    • Group /solutions/group/term
    • Categories /solutions/categories/term
    • Sub Categories /solutions/sub-categories/term

    It'd be neat if I didn't need to expose the taxonomy name in the URL

    The custom route options for collections work really well, could something similar be applied to taxonomy routes in a future release?

  18. jasonvarga commented on Apr 16, 2021

    @jasonvarga
    Member

    I'd like to support it at some point since people clearly want it. I don't have an ETA though.

    I've fixed Steven's issue with the docs by removing it. 😄

  19. 1stevengrant commented on Apr 16, 2021

    @1stevengrant
    Contributor

    😂

  20. 1stevengrant commented on Apr 16, 2021

    @1stevengrant
    Contributor

    @stephenmeehanuk here's how I've worked around it

    Route::get('whats-on/festival/{slug}', function ($slug) {
    
        $festival = Term::findBySlug($slug, 'festival');
    
        return (new \Statamic\View\View)
            ->layout('layout')
            ->template('events.festival')
            ->with(['title' => $festival->title()]);
    });
  21. jasonvarga commented on Jan 28, 2022

    @jasonvarga
    Member

    Here's a gist with an example of how to implement custom taxonomy routes. It builds on what Steven posted.

    https://gist.github.com/jasonvarga/4342f96b77d52fcd27dad22d88253e1f

  22. warkior commented on Feb 6, 2023

    @warkior
    Author

    The various comments above have been very helpful. Here's a way we've solved it for now. We've added the following to our routes/web.php

    Route::get('magazine/topics/{slug}', function ($slug) {
    
        /** @var \Statamic\Taxonomies\LocalizedTerm $term */
        $term = Term::findBySlug($slug, 'magazine_topic');
        if( !$term )
            return response((new \Statamic\View\View)
                ->layout('layout')
                ->template('errors.404'), 404);
    
        return (new \Statamic\View\View)
            ->layout('layout')
            ->template('magazine_topic.show')
            ->with([
                'title' => $term->title(),
                'slug' => $slug,
            ] + $term->data()->toArray());
    
    });

    This allows page loads of /magazine/topics/something to load the something term, show a custom 404 if it's missing, and show the view with all term data if it exists. For clarity our term handle is actually magazine_topic.

    We are then able to use the following in views/magazine_topic/show.antlers.html.

    {{ collection from="magazine_articles" taxonomy:magazine_topic="{slug}" paginate="true" as="posts" }}
      ...
      {{ posts }}
        <h3> {{ title }} </h3>
        ...
      {{ /posts }}
      ...
    {{ /collection }}

    Hope this is helpful to someone out there. :)

  23. iipavlov commented on Nov 6, 2023

    @iipavlov

    I have the strange case where on my development and staging environments the collection-name/taxonomy/term path works fine with the custom templating, but on the production environment same path returns 404 page.
    The content, views etc between all environments are synced (merged branches).

    Where should I look for the issue?

  24. jackmcdade commented on Feb 15, 2026

    @jackmcdade
    Member

    Closing in favor of statamic/ideas#839. We're right on the fence between deprecating Taxonomies entirely and going all in on Collections, or doing the big work. We'll see. But this is an old issue and I'm trying to clean everything up. 😅

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions