Skip to content

Instantly share code, notes, and snippets.

@hkan
Last active November 6, 2015 10:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hkan/7913cb3c0f6939c6a055 to your computer and use it in GitHub Desktop.
Save hkan/7913cb3c0f6939c6a055 to your computer and use it in GitHub Desktop.
Reproduce routes for other languages in the application. (Laravel 4)
// I composed them into my layout file
View::composer('layout', function($view)
{
// Current route object
$route = Route::getCurrentRoute();
// Take out first part of route (the locale key)
$routeWithoutLang = implode('.', array_slice(explode('.', $route->getName()), 1));
// You should probably put this in a foreach loop
$langLink = [
'tr' => call_user_func_array(array('URL', 'route'), array('tr.' . $routeWithoutLang, $route->parameters())),
'en' => call_user_func_array(array('URL', 'route'), array('en.' . $routeWithoutLang, $route->parameters())),
'de' => call_user_func_array(array('URL', 'route'), array('de.' . $routeWithoutLang, $route->parameters()))
];
$view->with('langLink', $langLink);
});
Route::group([ 'prefix' => 'tr' ], function ()
{
Route::get('/', [ 'as' => 'tr.home', 'uses' => 'HomeController@showWelcome' ]);
Route::get('urunler', [ 'as' => 'tr.products.index', 'uses' => 'ProductsController@index' ]);
Route::get('urunler/{group}', [ 'as' => 'tr.products.group', 'uses' => 'ProductsController@group' ]);
Route::get('urunler/{group}/{product}', [ 'as' => 'tr.products.product', 'uses' => 'ProductsController@product' ]);
});
Route::group([ 'prefix' => 'en' ], function ()
{
Route::get('/', [ 'as' => 'en.home', 'uses' => 'HomeController@showWelcome' ]);
Route::get('product', [ 'as' => 'en.products.index', 'uses' => 'ProductsController@index' ]);
Route::get('product/{group}', [ 'as' => 'en.products.group', 'uses' => 'ProductsController@group' ]);
Route::get('product/{group}/{product}', [ 'as' => 'en.products.product', 'uses' => 'ProductsController@product' ]);
});
Route::group([ 'prefix' => 'de' ], function ()
{
Route::get('/', [ 'as' => 'de.home', 'uses' => 'HomeController@showWelcome' ]);
Route::get('produkte', [ 'as' => 'de.products.index', 'uses' => 'ProductsController@index' ]);
Route::get('produkte/{group}', [ 'as' => 'de.products.group', 'uses' => 'ProductsController@group' ]);
Route::get('produkte/{group}/{product}', [ 'as' => 'de.products.product', 'uses' => 'ProductsController@product' ]);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment