-
-
Notifications
You must be signed in to change notification settings - Fork 1.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
4.x - Get base path within a route callback #2837
Comments
Any other ideas or opinions on that? |
@adriansuter What would be the difference compared to the existing Uri::getBaseUrl? https://github.com/slimphp/Slim-Http#decorated-uri-object-methods
I think first we should get a better overview of what we have now (and where) and point out the differences and uses cases for each of that. |
@odan if your app lives in a subdirectory, say |
@adriansuter I think we could maybe provide the basepath via middleware when routing is done? Then maybe add onto the |
@adriansuter The currently possible way would be to define a "name" for the $app->setBasePath('/app');
$app->get('/', \App\Action\Home\HomeAction::class)->setName('root');
$app->get('/users', \App\Action\User\ListUserAction::class)->setName('list-user'); Then you can fetch a url path (with the baseBath) like this: // Result: /app
$url = RouteContext::fromRequest($request)->getRouteParser()->urlFor('root');
// Result: /app/users
$url = RouteContext::fromRequest($request)->getRouteParser()->urlFor('list-user'); Using the route Edit: This works fine for existing routes, but we might want to create a dynamic route. In this case @l0gicgate idea with a |
I think RouteParser should implement basePath method. IMHO RouteParser class is good place for it as
However I'd suggest method with no arguments to keep it as simple as possible. class RouteParser
{
// ...
public function basePath(): string
{
$this->routeCollector()->getBasePath();
}
} One can always use Another helpful method would be <html>
<head>
<base href="<?= $this->routeParser->baseUrl() ?>/" />
</head>
<body>
<a href="<?= $this->routeParser->relativeUrlFor('about-us') ?>">about us</a>
<img src="./image.png" />
</body>
</html> |
@piotr-cz Keep it simple is good. The thing is, if you are developing an app and always forget to prepend My idea was to kinda force the developer to use the This does not mean, that we should not have a method to get the base path or the base url. In fact, I would even suggest to implement all four methods. What do you think @l0gicgate? Too much? |
@adriansuter I get your point. In that case it's more convenient to use one of the view libraries (PHP-View or Twig-View) and implement these methods in them (or in extending classes). But to do so, there must bu a way to retrieve I think that one thing we both agree on is that there should be a method to get basePath similarly like we get routes. I suggest that once such implementation lands in codebase we can discuss other methods. |
How about RouteContext::fromRequest($request)->getBasePath() : string As // \Slim\Routing\RouteContext
private function __construct(
?RouteInterface $route,
RouteParserInterface $routeParser,
RoutingResults $routingResults,
string $basePath
) { But to be able to develop this method, we have to bring the base path into the // \Slim\Middleware\RoutingMiddleware
public function __construct(RouteResolverInterface $routeResolver, RouteParserInterface $routeParser, string $basePath) @l0gicgate How about using the |
Need to add docs for this on Slim-Website repo |
Say you have a static file for which you would like to build the url. So the url is not a route. As there might be a base path present, we can't just write
/static-file.html
. How do we get the base path or even the base url from within a route callback?Of course we could inject the base path to the class containing the route callback. But there should be a better solution.
Something like
I am not sure if that belongs to the route parser though. If it does, we could maybe instead of the above create two methods that are similar to
urlFor
andfullUrlFor
but instead of using a route name, we could pass a path (relative to the application end point).So
What do you think?
The text was updated successfully, but these errors were encountered: