Skip to content

Instantly share code, notes, and snippets.

@ponich
Created November 25, 2020 10:41
Show Gist options
  • Select an option

  • Save ponich/9b5b6aaa35ed6ce54670731219b6d820 to your computer and use it in GitHub Desktop.

Select an option

Save ponich/9b5b6aaa35ed6ce54670731219b6d820 to your computer and use it in GitHub Desktop.
laravel
<?php
if (!function_exists('getRouteUriByName')) {
/**
* Get route URI by route name
*
* @param string $name
* @return string
* @throws Exception
*/
function getRouteUriByName(string $name): string
{
/**
* @var RouteCollection $routes
*/
$routes = app('router')->getRoutes();
/**
* @var Route|null $router
*/
$router = $routes->getByName($name);
if (!$router) {
throw new \Exception("Could not find route by name `{$name}`");
}
return Str::before($router->uri(), '{');
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment