Prefered way to handle URLs (slugs, entities, router idea)
- Climber007
- Member | 105
Hi everyone,
I found some forum topics and private blog posts with advices how to make cool router with parameter to entity conversion but I can't still decide what is the right way.
The core router idea is to separate logic of mathing and creating URLs from entire application. But there are lots of questions!
So, you have the most basic route with pattern
<presenter>/<action>/<id>
. It is common to pass
the identifier id
to presenter's action as argument and find there
the required „entity“ by this identifier. When entity doesn't exist you just
redirect to 404.
You can find advices to make router more cool and find the entity right in the router. For the first time it sounds me a great idea but there are some reasonable questions.
Isn't it connectiong router with application and model? But as it is said router should be separated from the app.
Is it good to redirect in router when invalid identifier occurs? It won't be done as everybody is used to in action method. You lose possibility of making your of error message.
When there is an entity in roture is easier to keep idea of separating construction and matching URL when you want to work with slugs. On the way in the application router pass the entity. The app don't know there was any slug in the URL instead of integer identifier. On the way from the application you just pass entity to LinkGenerator and router decides what appers in URL – if slug or integer.
There are some cons a pros for extending the router by „entity logic“. But isn't it a breaking the router basic idea? I still keep in mind the router just shoud decide what presenter and action is going to be perform. Everything else should be done in there. But this way my action should know what is in URL – integer or slug? Application should be independent on router, grrr.
Same problem is on the way out the application. I'd make link by common way
Cool:action $id
but now router have to find slug for this
$id. Also I don't want to make decision if pass the integer or slug to
link. When I pass entity insted of integer $id route gets the
slug from the entity, grrr.
So, now I'm not able to decide what is the right way. For the easy explanation of my problem I've used the most basic URL pattern however you can have same problems with more complex URL in large application.
Thank you for the discusion!
- Eda
- Backer | 220
I am a fan of having entities as arguments in action methods. And it leads to that connecting entity finding etc. in router.
I believe its the most correct solution for my needs. Routing is as much separated as possible. And its supernice to call actions directly with arguments of entities.
It's not true, that you cannot then show custom 404 – you can add special route for it for example. Or distinguish it in Error presenter / or its template.
On the other hand, it has some problems. Some of them we discussed some time
ago there (CZ):
https://forum.nette.org/…ekavane-pole#…
Last edited by Eda (2021-02-07 00:50)