Closed greatislander closed 3 years ago
Describe the bug
When current_route() with a fallback is used in a template that is rendered on a 404 page, the helper throws an error rather than using the fallback route:
current_route()
Call to a member function getName() on null
This is caused by the $route->getName() call on line 25: https://github.com/chinleung/laravel-multilingual-routes/blob/47683d255325fd825ca5da893e9d8fae02844f05/src/helpers.php#L21-L26
$route->getName()
However, on a 404 page the result of Route::getCurrentRoute() is null so the method fails.
Route::getCurrentRoute()
null
To Reproduce
Steps to reproduce the behavior:
Expected behavior
The fallback should be returned if the current route is null.
Result of route:list:
Not applicable.
Additional context
Adding the following code appears to resolve the issue:
$route = Route::getCurrentRoute(); + if (! $route) { + return $fallback; + } $name = Str::replaceFirst( locale().'.', "{$locale}.", $route->getName() );
If this makes sense, I'd be happy to open a pull request.
Hey @greatislander,
This looks good to me! Could you submit a pull request with the test please?
Thanks!
Thanks @greatislander! This is fixed as of v2.7.4.
Describe the bug
When
current_route()
with a fallback is used in a template that is rendered on a 404 page, the helper throws an error rather than using the fallback route:This is caused by the
$route->getName()
call on line 25: https://github.com/chinleung/laravel-multilingual-routes/blob/47683d255325fd825ca5da893e9d8fae02844f05/src/helpers.php#L21-L26However, on a 404 page the result of
Route::getCurrentRoute()
isnull
so the method fails.To Reproduce
Steps to reproduce the behavior:
current_route()
with a fallback.Expected behavior
The fallback should be returned if the current route is
null
.Result of route:list:
Not applicable.
Additional context
Adding the following code appears to resolve the issue:
If this makes sense, I'd be happy to open a pull request.