Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Wrong behaviour for LaravelLocalization::extractAttributes() => redirects to 404 #735

Open
Nuranto opened this issue Jul 28, 2020 · 2 comments

Comments

@Nuranto
Copy link

Nuranto commented Jul 28, 2020

Redirection from non-localized URLs to localized URLs does not work sometimes, because of LaravelLocalization::extractAttributes() behaviour : it does not match domain restrictions.
(More generally, getLocalizedURL does not work because of that.)

For instance :

Route::domain('www.toto.com')->group(function() {
     Route::get('tutu/{slug}', 'TotoController@toto')->name('toto');
});
Route::domain('www.tata.com')->group(function() {
     Route::get('tutu/titi', 'TataController@tata')->name('tata');
});

If you try to load https://www.tata.com/tutu/titiit will match toto route instead of tata route, and will redirect to : https://www.tata.com/en/tutu/{slug} instead of https://www.tata.com/en/tutu/titi !

Isn't it possible to get the route directly from laravel methods ? That way, we would be sure the route found is the correct one, even for futures Laravel updates on routing..?

Maybe like this ?

protected function extractAttributes($url = false, $locale = '')
    {
        if (!empty($url)) {
             $route = collect(\Route::getRoutes())->first(function($route) use($url){
                  return $route->matches(request()->create($url));
             });
            /* generate $attributes from found $route */
[...]

where $url is the absolute url.

(credits : I found that solution here => https://stackoverflow.com/questions/24582922/laravel-get-route-name-from-given-url)

@Nuranto
Copy link
Author

Nuranto commented Aug 18, 2020

Here is the full solution, but it gets slow..

protected function extractAttributesNew($url = false, $locale = '')
    {
        $route = false;
        if (!empty($url)) {
            $route = collect(\Route::getRoutes())->first(function($route) use($url){
                  return $route->matches(request()->create($url));
             });
            if($route) {
                $attributes = $this->normalizeAttributes((new \Illuminate\Routing\RouteParameterBinder($route))
                        ->parameters(request()));
            }
        } else {
            $route = $this->router->current();
            $attributes = $this->normalizeAttributes($route->parameters());
        }
        if (!$route) {
            return [];
        }

        
        $response = event('routes.translation', [$locale, $attributes]);

        if (!empty($response)) {
            $response = array_shift($response);
        }

        if (\is_array($response)) {
            $attributes = array_merge($attributes, $response);
        }

        return $attributes;
    }

Nuranto added a commit to Nuranto/laravel-localization that referenced this issue Aug 18, 2020
@Nuranto
Copy link
Author

Nuranto commented Aug 18, 2020

@mcamara I PR a mixed version between the one above, and yours... You'll tell me.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant