chinleung / laravel-multilingual-routes

A package to handle multilingual routes in your Laravel application.
https://github.com/chinleung/laravel-multilingual-routes-demo
MIT License
394 stars 26 forks source link

On 404 page, current_route with fallback throws an error #54

Closed greatislander closed 3 years ago

greatislander commented 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:

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

However, on a 404 page the result of Route::getCurrentRoute() is null so the method fails.

To Reproduce

Steps to reproduce the behavior:

  1. Try to access a non-existent page using a template which includes current_route() with a fallback.
  2. See error.

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.

chinleung commented 3 years ago

Hey @greatislander,

This looks good to me! Could you submit a pull request with the test please?

Thanks!

chinleung commented 3 years ago

Thanks @greatislander! This is fixed as of v2.7.4.