1
votes

I have defined routes this way:

/*
 * Set up route patterns - patterns will have to be the same as
 * in translated route for current language
 */
foreach(Lang::get('routes') as $k => $v) {
    Route::pattern($k, $v);
}

Route::group(array('prefix' => Config::get('app.locale_prefix')), function()
{
    Route::get('/{login}/', ['as' => 'login', 'uses' => 'LoginController@showLogin']);
});

Depending on selected language {login} parameter will become login in English or for example logowanie in Polish and routes are working fine this way.

However I have problem with creating redirects to named routes.

If I simply use:

Redirect::route('login'));

if will redirect me to http://localhost/{login} url - parameter won't be changed into login or logowanie depending on current language.

On the other hand if I use:

Redirect::route('login', Lang::get('routes'));

and of course in routes.php file I have many routes it will create the following url:

http://localhost/logowanie?register=rejestracja&dashboard=konto&logout=wyloguj

so it will handle correct {login} parameter but it will add other array elements to url in query string.

Of course I could use in this case just one element of routes but it will make that for each routes I will have to manually pass selected parameter and not the whole array.

Question - is it possible to pass the whole array of routes as above but make somehow Laravel only to handle parameters that are included in route without adding query string?

2

2 Answers

1
votes

I am not sure i understand you correctly, but you could always define only one translated route.

Route::get('/'.Lang::get('routes.login'), ['as' => 'login', 'uses' => 'LoginController@showLogin']);

and

Redirect::route('login');

Update And please be aware that Lang::get('routes') gets the whole array of route translations (at least i assume you defined it that way). In your second attempt it should work with

Redirect::route('login', Lang::get('routes.login'));
0
votes

What I finally did:

I created Redirection facade (not Redirect but Redirection) - this is because it seems quite hard to extend both Redirect and UrlGenerator (They are in Routing Service Provider) and it would be necessary in this case.

Redirection class created by facade is defined in the following way:

<?php

namespace Utils;

use \Lang;
use \URL;
use \Redirect;


use Illuminate\Routing\UrlGenerator;

class Redirection
{


    /**
     * Create a new redirect response to a named route and automatically adds
     * translated route variables.
     *
     * @param  string $route
     * @param  array  $parameters
     * @param  int    $status
     * @param  array  $headers
     *
     * @return \Illuminate\Http\RedirectResponse
     */
    public function route(
        $route,
        $parameters = array(),
        $status = 302,
        $headers = array()
    ) {
        $routeUrl = urldecode(URL::route($route));

        preg_match_all('/{(.*)}/', $routeUrl, $matches);

        foreach ($matches[1] as $parameter) {
            $parameters[$parameter] = Lang::get('routes.' . $parameter);
        }

        return Redirect::route($route, $parameters, $status, $headers);
    }
} 

Now if I want to make redirection to named route I don't use:

 Redirect::route('login');

but

 Redirection::route('login');

and all required routes are passed as route method parameters to Redirect facade.