Console - RouteContext

Hi all,

I’m busy implementing a command via console.
However, I would need to generate a URL via RouteContext.
The problem is that I have to give “$request” as a parameter, which I don’t have via the command.

Do you have any idea how I can generate/recover it?

Thanks

This depends on the installed PSR-7 package. It would be helpful, if you could provide for information / details. If you have access to the Slim App instance, you could use that without a request object.

$routeParser = $app->getRouteCollector()->getRouteParser();
$url = $routeParser->urlFor('route-name');

Simple as that…
Work like a charm, thanks

If a need the full URL, I think I should write it in the setting config cause in console there is no URI, I’m right ?

You can then create a Uri object and pass the domain. The value for the domain can be fixed or come from a configuration setting.

$domain = 'https://example.com';
$url = $routeParser->fullUrlFor(new \Nyholm\Psr7\Uri($domain), 'route-name');

I see that you’re using “Nyholm”, I’m working with “Slim”.

Should I use this ? Also, I’ll need to change a lot of code ?

Thanks

Yes, you could also use the Slim PSR-7 Slim\Psr7\Uri class. You don’t have to refactor everything.

Yes, that’s what I imagined.
But I misspoke, is there an advantage to using Nyholm rather than Slim?
Refactoring doesn’t bother me, I’m starting a project.

Thank you

For me, there are some advantages of using Nyholm.

The Nyholm implementation better maintained and 100% compatible according to the specification. See here:

Nyholm is also faster: GitHub - devanych/psr-http-benchmark: Benchmark of PSR-7 and PSR-17

1 Like

Hi Odan, it’s ok and that’s working fine, not a big deal to change it.

Thanks for your advice.