I try to find name of previous route in Laravel 5.1. With:
{!! URL::previous() !!}
I get the route url, but I try to get route name like I
I've created a helper function like this.
/**
* Return whether previous route name is equal to a given route name.
*
* @param string $routeName
* @return boolean
*/
function is_previous_route(string $routeName) : bool
{
$previousRequest = app('request')->create(URL::previous());
try {
$previousRouteName = app('router')->getRoutes()->match($previousRequest)->getName();
} catch (\Symfony\Component\HttpKernel\Exception\NotFoundHttpException $exception) {
// Exception is thrown if no mathing route found.
// This will happen for example when comming from outside of this app.
return false;
}
return $previousRouteName === $routeName;
}