HackerOne users: Testing against this community violates our program's Terms of Service and will result in your bounty being denied.
How to remove the discussion ID from the URL. Super clean pretty url!
Greetings,
In this thread I share a little idea/tut on how to create a tiny plugin to manage an app-level rerouting. This is only preferable for minimal use, say like what the plugin "BasicPages" has in mind. So, beware, cavete!!!
Like example.com/about-us, example.com/faq, and so on.
Question:
I am playing with discussionModel_beforeSaveDiscussion_handler
Is it possible to get the id that vanilla has/will determine for a new discussion?
I am trying to get create a route, and need the id.
Thanks.
Tagged:
0
Comments
Since it is the "before" save event, it is not possible. You need the AfterSaveDiscussion event for that. Maybe there is another approach? Could you describe in more detail what you are trying to achieve?
I have a script that manages rerouting super clean urls to my discussions.
I removed the discussion ID from the url slug altogether and so this script internally checks the config for existing rules, conflicts and updates etc.
All this works nicely for existing discussions, not so much for newly created ones.
I need the id to set in Route target discussion/id$
And I did try AfterSaveDiscussion before but it causes a big 500 error on the whole site, so I ran far away from that option. I tried to troubleshoot but it started to eat up all my time.
Okay. I went back to crack my errors, seems I had two AfterSave functions firing. Working now.
Thanks R_J for looking.
All is well that ends well!
How do you deal with discussions that have the same title?
It's definitely preferable to use the discussion ID. Not only is guaranteed to be unique, but it's also a very fast lookup. Is the plugin open source? I'm always curious to see different routing strategies people are using. We've quite a few discussions about routing internally recently.
Absolutely @charrondev . The purpose of this exercise was to allow part of the website act as ordinary site pages (about-us, faq etc), much like what this plugin does (BasicPages).
The way routes are currently handled both in UI and in writing/saving to config.php makes it very cumbersome to desire/attempt to replace completely the existing routing procedure.
One hundred entries and we would be heading to the pharmacy. Maybe in future, if this ever gets seriously requested, Vanilla could store this kind of thing in the DB, GDN_Discussion Table, and do some serious caching in the mapping, of course all the caveats still in place. Forums can easily rack up discussions. Unlike general CMSes.
@R_J and @charrondev I added a new customPages field to the Categories table. If a discussion belongs there then the magic happens. Also, this customPages attribute is considered in the smarty template, so that these discussions can have a totally different look from the normal discussionisque style.
Here is the function, maybe this can be posted in the tutorial section?
It will not allow duplicate of route regardless of destination.
It can allow multiple routes to same destination.
It will check if user is changing the discussion name, delete the old
And of course all the other places where we need the new pretty urls would have to be altered, some setData() etc ...
Most notably is the DiscussionUrl function.
We might also need to do some cleanup when a discussion is deleted.
The DiscussionModel has the following lines:
Therefore you can use it like that:
I wouldn't add the "ActivateCustomPages" as a Category property but make that a config setting "YourPlugin.CategoryIDs" = '1,2,3'. That would allow a setting page to configure it easily.
And without understanding your getRouteByTarget method: couldn't you use Gdn::router()->getRoute() or getDestination() for that purpose?
You have chosen "Internal" as the redirection method. I would have chosen "301 permanent", since the discussion should never again be accessed as e.g. "discusion/123/about-us" but always as "about-us". But I don't know if this would really make any difference. It just feels "more exact"
If not all discussions in a category get a new route, I would add the route as an information to the discussion attributes to be able to quickly check for any needed actions. That would avoid having to do route lookups.
Thanks @R_J for the suggestions.
IIRC, getDestination() does not get the whole route array. My function gets the array via the destination, so that I can check against the route name and other properties.
I tried the 301 permanent, and to say the least, absolute disaster! The requests forwards to /discussion/id# totally ignoring the pretty url. And that stuff is pretty permanent, I know there is got to be a way to undo it, but it is not clear. My browser keeps forwarding even after I changed it in the vanilla settings and clean vanilla cache ...
If not all discussions in a category get a new route, I would add the route as an information to the discussion attributes to be able to quickly check for any needed actions. That would avoid having to do route lookups.
This I find super interesting ... I will have to try it.
Thanks again.
I just googled a bit and everyone who knows something about redirects says "use 302 if you are not 100% sure": sorry for my bad advice.
301 redirects are cached in your browser but you might need to search the internet for how to remove those locally cached redirects from your browser. These seems to be non-trivial
The only way to properly clear a 301 is to issue a 301 in the opposite direction, they'll then cancel out for most clients. Be very careful with 301s though. The last thing you want is a crawler (like googlebot) to cache a bunch of 301s to the wrong place.