The Slim Router (the same as FastRoute?) seems to support routes only with leading slash (e.g. /home
).
This is fine when the app is directly accessible at a domain (e.g. http://localhost/home
, https://example.com/home
) but it provokes problems when serving the app at a sub-path of a domain (e.g. http://localhost/myapp/home
, https://example.com/myapp2/home
).
This limits the possibilities, forcing the app to always generate absolute-path references (e.g. /myapp2/home
, by configuring basePath = "/myapp2"
), instead of allowing to use the simpler relative-path references (e.g. home
).
It’s curious that this doesn’t seem to be addressed anywhere; as a microframework I would expect Slim to adapt to how the dev desires to structure the linking model of their webapp.
For my app I want to generate relative-path URIs instead.
When /myapp2/home
page is loaded in the browser, then a link to the About-page should be generated as href="about"
instead of href="/about"
, because:
about
will load relatively to/myapp2/home
, ✅ correctly leading to/myapp2/about
./about
will load relatively to the host root path/
, ❌ wrongly leading to/about
(that is outside of the namespace of my app!).
I’d rather specify a base
tag with href="myapp2/"
(or, better, no base-tag at all), rather than configuring $app->setBasePath("myapp2")
(and resolving server-side what the basepath value must be).
How can this be achieved with Slim 4 Router?
How to bend Slim to generate relative-path references without the initial slash?
Should the router be overloaded to do some dirty string replace/trimming?