Is it possible to get tha path with domain in Slim 3? For example this method $this->router->pathFor(‘redirect_back’) returns only /back, but I need site.dev/back.
You can work it out from the request object using methods like getScheme
, getHost
, and getPort
. See the Request URI section of the docs for more information and examples.
I’m using slimphp/Twig-View in many of my projects and I setup things like that and the site title, etc, within my Twig settings so they are available in all of my views. Using akrabat’s slim3-skeleton as an example, in settings.php my Twig settings would look something like this.
'view' => [
'template_path' => __DIR__ . '/templates',
'twig' => [
'cache' => __DIR__ . '/../cache/twig',
'debug' => true,
'auto_reload' => true,
],
'app' => [
'siteName' => 'Awesome Site',
'baseUrl' => 'http://example.com',
],
// ...
With that in place I can use them within my Twig views like this:
<h1>Welcome to {{ app.siteName }}</h1>
<p>Please <a href="{{ app.baseUrl }}{{ path_for('contact') }}">contact us</a>.</p>
What I like about doing it this way is that I typically have local, staging, and development versions of my sites which will have a different domain and site name, so it feels natural to have them in my settings which is not under version control since they are largely specific to an environment.
Yep, with some extra code it’s possible, but for me it’s strange why it cant be done “out the box”…
The most common scenario is probably that the developer is using relative URIs for everything. This is also convenient for development where the testing URI might be http://example.localhost
and production is at http://example.com
. If you’re using relative links then you don’t need to change anything.
In cases where you can’t use a relative link, you might need additional logic anyway to determine the correct scheme, host, etc which the framework wouldn’t likely be able to determine on its own.
Otherwise, you could setup something simple yourself in the container with a line or two of code; it isn’t much.
You could always raise an issue at Github, see what the response is from others, or submit a pull request with the feature.