Get URI from route name
Hi,
while there is a UrlGenerator which converts route name to URL, is there a way to easily get URI?
Lots of options, like Request::url(), Request::fullUrl(), Request::path().
Sorry @JeffreyWay :) your reply is great, I didn't know all those methods.
Thanks
Route::getResourceUri(); is what you are looking for possibly?
Hi @keevitaja,
Using the method inyection with the new Router class I would make as follow.
In the BaseController you can add the following method:
public function routeToURI(Router $router)
{
return str_replace('.', '/', $router->currentRouteName());
}
Don't forget import the Illuminate\Routing\Router class.
You could make the full path using the request object.
@codeatbusiness route name and uri do not have to match! So this solution does not do it!
@TravisBlasingame this gives me weird result: reminders/{reminders}/token/{token}/create
Without stuff between {} it would be ok.
edit: Currently i get the URL and remove the protocol and domain. So i can get the URI... Just wondering if there is a better solution.
\URL::route($route, [], false);
The false is for the $absolute flag in the UrlGenerator. Without absolute, it just returns the Uri.
$router = app()->make('router');
dd($router->getCurrentRoute()->uri);
... from 2 years ago, but in Laravel 5.4 that's a way to obtain the uri :)
Neigher of methods worked for me in Laravel 5.5. I was able to find URI by this:
uri_for(route('ua.index'))->getPath();
Laravel 5.7:
route('ua.index', [], false);
Please or to participate in this conversation.