1

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?

Marcin Nabiałek
  • 109,655
  • 42
  • 258
  • 291

2 Answers2

1

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'));
Sloothword
  • 349
  • 1
  • 10
  • That's probably true I can define them this way but it won't be so legible if I have many routes. I am aware that `Lang::get('routes')` give me the whole array but I would expect `Redirect` use only those that are in the Route and not create queryString after `{login}` part. I mentioned it in my question. – Marcin Nabiałek Sep 05 '14 at 06:16
  • I guess thats not possible as Laravel assumes all parameters in the `Redirect::route('login', $parameters);` call not used in the route are GET parameters. I also cannot see how my route definition is in any way longer than what you did. Maybe make an example with many routes. Additionally it is a lot cleaner if you don't touch translations etc. at all for creating routes. – Sloothword Sep 05 '14 at 12:38
  • Well, but my main point is to have translated routes in url for each language. I use a method described here: http://stackoverflow.com/q/25082154/3593996 – Marcin Nabiałek Sep 08 '14 at 11:22
  • I see where you are coming from. But with that method i guess it is essential to manually specify the `{login}` variable with `Redirect::route('login', ['login'=>'loginInAnyLanguage']);`. Therefore I gave an alternative way to define translated routes without the need for that. If you have specific problems with that, i will help gladly. Otherwise maybe you ask the original author of your solution how he would generalte links. – Sloothword Sep 08 '14 at 15:44
0

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.

Marcin Nabiałek
  • 109,655
  • 42
  • 258
  • 291