Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Custom Taxonomy Routes #2403

Open
warkior opened this issue Sep 10, 2020 · 26 comments · May be fixed by #8627
Open

Custom Taxonomy Routes #2403

warkior opened this issue Sep 10, 2020 · 26 comments · May be fixed by #8627

Comments

@warkior
Copy link

warkior commented Sep 10, 2020

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

@mikemartin
Copy link

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.

@JonKaric
Copy link
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.

@FrankPeters
Copy link

FrankPeters commented Nov 25, 2020

+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.

@FrankPeters
Copy link

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.

@JonKaric
Copy link
Contributor

JonKaric commented Nov 26, 2020

@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.

@FrankPeters
Copy link

@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 ;-)

@duncanmcclean
Copy link
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.

@github-actions
Copy link

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.

@JonKaric
Copy link
Contributor

JonKaric commented Feb 2, 2021

Re-opening this

@1stevengrant
Copy link
Contributor

Docs suggestion being able to define the route in the YAML file for the taxonomy https://statamic.dev/knowledge-base/taxonomies-by-hand#creating-taxonomies but that doesn't work

title: 'Stevens Tags'
route: 'abc/sg/{slug}'

However when I loop through the tags

{{ stevens_tags }}
        {{ url }}
    {{ /stevens_tags }}

the output is still /stevens_collection/stevens-tags/tag

@stephenmeehanuk
Copy link

stephenmeehanuk commented Apr 13, 2021

I'm hitting this same problem. Is there a fix on the horizon?

Is the issue related to the need for a taxonomy subfolder in a collection?

@jasonvarga
Copy link
Member

You're able to have collection-specific taxonomy routes.

https://statamic.dev/taxonomies#templating

e.g. if you create resources/views/blog/tags/index.antlers.html, then /blog/tags will be available.
and resources/views/blog/tags/show.antlers.html will make /blog/tags/mytag will be available.

@1stevengrant
Copy link
Contributor

yeah, that works but if you wanted to customise the route further?

I have a site on v2 that has a particular routing structure they don't want to move from. I've been able to circumvent via some gnarly route file magic.

@jasonvarga
Copy link
Member

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

@1stevengrant
Copy link
Contributor

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

@stephenmeehanuk
Copy link

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?

@jasonvarga
Copy link
Member

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

@stephenmeehanuk
Copy link

@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?

@jasonvarga
Copy link
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.

@stephenmeehanuk
Copy link

stephenmeehanuk commented Apr 16, 2021

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?

@jasonvarga
Copy link
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. 😄

@1stevengrant
Copy link
Contributor

😂

@1stevengrant
Copy link
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()]);
});

@jasonvarga
Copy link
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

@warkior
Copy link
Author

warkior commented Feb 6, 2023

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. :)

@iipavlov
Copy link

iipavlov commented Nov 6, 2023

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?

# for free to join this conversation on GitHub. Already have an account? # to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

10 participants