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

Route Translation with Model Binding not working #268

Closed
ndberg opened this issue Dec 8, 2015 · 14 comments
Closed

Route Translation with Model Binding not working #268

ndberg opened this issue Dec 8, 2015 · 14 comments

Comments

@ndberg
Copy link

ndberg commented Dec 8, 2015

I have a issue with route translation with Model Binding, the route translation isn't working then:

Http/routes.php

Route::get(LaravelLocalization::transRoute('routes.firma/abdeckung/id'), ['uses' => 'FirmaController@abdeckung']);

en/routes.php

"firma/abdeckung/id" => "company/coverage/{firma}",

de/routes.php

"firma/abdeckung/id" => "firma/abdeckung/{firma}",

I use Model Binding in app/Providers/RouteServiceProvider.php

$router->model('firma', '\App\Firma');

Now if I am on the German site and push the link for English, it redirects me from:

de/firma/abdeckung/2

to

en/firma/abdeckung/2

instead of

en/company/coverage/2

It looks like its a Problem of the model binding, because if i take the model binding out, it works perfectly. Does anyone know something about this?

(I know there is also the possibility to translate the attributes, but I thought I will first get this to work without it)

@BojanKogoj
Copy link
Contributor

@foreach(LaravelLocalization::getSupportedLocales() as $localeCode => $properties)
<a href="{{LaravelLocalization::getLocalizedURL($localeCode) }}">{{$localeCode}}</a><br />
@endforeach

This should translate current URL completely

@ndberg
Copy link
Author

ndberg commented Dec 8, 2015

Great thanks a lot, works like a charm

@ndberg ndberg closed this as completed Dec 8, 2015
@ndberg
Copy link
Author

ndberg commented Dec 8, 2015

sorry, I was to fast.. I forgot to reactivate the model binding in the service provider.. It's still not working..

@ndberg ndberg reopened this Dec 8, 2015
@ndberg
Copy link
Author

ndberg commented Dec 10, 2015

Does no one of you guys use model binding?

@arcanedev-maroc
Copy link

I don't know if the problem is caused by using the model binding.

You need to edit your routes.php in lang folders, instead of using keys with /, try to do something like this :

// resources/lang/de/routes.php

return [
    'firma-abdeckung-id' => 'firma/abdeckung/{firma}',
    // LaravelLocalization::transRoute('routes.firma-abdeckung-id')
];

// OR like this (I don't know if this package support it or not)
return [
    'firma' => [
        'abdeckung' => [
            'show' => 'firma/abdeckung/{firma}',
            // LaravelLocalization::transRoute('routes.firma.abdeckung.show')
        ],
    ],
];

// Same thing for other locales

You can also check your model binding by doing this:

$router->bind('firma', function($id) {
    $record = \App\Firma::where('id', $id)->first();

    dd($record); // This is only to see if everything is OK.

    return $record;
});

@ndberg
Copy link
Author

ndberg commented Dec 11, 2015

Thanks for your response, I appreciate it.

I'm pretty shure that it's caused by the model binding, because if I take it out, it all works great.. I tried your suggestion with using keys without '/', but it doesn't matter. And if I take the model binding out, it also works with keys with '/'.

Thanks for your check code for the model binding. Yes it works all great.

It's just that this function:

@foreach(LaravelLocalization::getSupportedLocales() as $localeCode => $properties)
<a href="{{LaravelLocalization::getLocalizedURL($localeCode) }}">{{$localeCode}}</a><br />
@endforeach

returns the false URL:

en/firma/abdeckung/2

instead of the translated

en/company/coverage/2

The Routing itself, works with all translated routes, means:

en/company/coverage/2
de/firma/abdeckung/2

@ndberg
Copy link
Author

ndberg commented Dec 11, 2015

I tried something..

If I call

 LaravelLocalization::getURLFromRouteNameTranslated('en', 'routes.firma-abdeckung-bindFirma')

Result:

http://torebuild1.local/en/company/coverage/{firma}

So this translation works, even with model binding.. this one not:

LaravelLocalization::getLocalizedURL('en')

Result:

URL: http://torebuild1.local/en/firma/abdeckung/2

So perhaps I can make a work-around with other functions from the package..

@BojanKogoj
Copy link
Contributor

How about

LaravelLocalization::getURLFromRouteNameTranslated('en', \Request::route()->getName())

@ndberg
Copy link
Author

ndberg commented Dec 11, 2015

Thanks, but it's not working, Result:

www.domain.com/en

I managed the links in the nav with this:

<a href="{{ LaravelLocalization::getLocalizedURL(LaravelLocalization::getCurrentLocale(), 'firma/abdeckung/{firma}', ['firma' => $firma->id]) }}">Gebiet</a>

But I still couldn't find a usable solution for the Language switcher, because of different model binding parameters in different routes.. Then I would have to get dynamically the name of the model binding parameter, I don't like that.. I'm sorry i've read my posts again and realized that I didn't wrote clearly that the problem is just with the language switcher.

Thank you all for any help

@arcanedev-maroc
Copy link

I don't know exactly why this is not working but i'm curious about this issue.

I've created my own localization package and i don't know if i've got the same issue.

You can try it if you want : https://github.com/ARCANEDEV/Localization

I can assist you to find out the issue and also to improve my package.

NOTE : My package was based on mcamara/laravel-localization, so they're gonna be only some minor changes to do.

@ndberg
Copy link
Author

ndberg commented Dec 11, 2015

Wow great, thank you very much for your support. I'll take a look at your package.

@arcanedev-maroc
Copy link

You're welcome 👍

@arcanedev-maroc
Copy link

I fixed the issue in my localization package.

To @mcamara, @BojanKogoj

When we use route bindings, you need to be aware that you can have classes/models when you grab attributes from the request at this line.

To fix it, you need to update the substituteAttributesInRoute() method:
https://github.com/mcamara/laravel-localization/blob/master/src/Mcamara/LaravelLocalization/LaravelLocalization.php#L546-L558

This is what i've done to fix it: ARCANEDEV/Localization#21

@ndberg
Copy link
Author

ndberg commented Dec 14, 2015

Thanks, your package works great.

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

3 participants