I work on a multi-lang webapp and therefore I also need to handle URLs translated to different languages (for SEO reasons). I'm following the guidelines from this article (second answer), but got stuck with a basic problem. Please see below my web.php:
<?php
use Illuminate\Support\Facades\Lang;
use Illuminate\Support\Facades\Route;
$all_langs = config('app.all_langs');
//Iterate over each language prefix
foreach( $all_langs as $prefix ){
Route::group(['prefix' => $prefix , 'middleware' => 'localization'], function() use ($prefix) {
if ($prefix == '') $prefix = 'en';
Route::get(Lang::get('routes.home',[], $prefix), 'HomeController@index')->name('home');
});
}
I though that the above code snippet will generate a route that is based on the german translation of 'home': for example de/hause (for testing purposes only).
Instead of this the route generated has the URI routes.home, feeling like that the translation did not work. Additionally my translation files (lang/en/routes.php and lang/de/routes.php) are printed out at the top of the page (why is this happening?). Please see the generated route:list illustrating this below:
Ors-MacBook-Pro:gng_backo Ors$ php artisan route:list
return array(
'home' => 'home',
'tours' => 'tours',
'guides' => 'guides'
'login' => 'login'
);return array(
'home' => 'hause',
'tours' => 'touren',
'guides' => 'stadtfuehrer'
);+--------+----------+----------------+------+-------------------------------------------+------------+
| Domain | Method | URI | Name | Action | Middleware |
+--------+----------+----------------+------+-------------------------------------------+------------+
| | GET|HEAD | api/user | | Closure | api |
| | | | | | auth:api |
| | GET|HEAD | de/routes.home | home | App\Http\Controllers\HomeController@index | web |
| | | | | | auth |
| | GET|HEAD | en/routes.home | home | App\Http\Controllers\HomeController@index | web |
| | | | | | auth |
+--------+----------+----------------+------+-------------------------------------------+------------+
REFERENCES:
My localization middleware:
<?php
namespace App\Http\Middleware;
use App;
use Closure;
class Localization
{
/**
* Handle an incoming request.
*
* @param \Illuminate\Http\Request $request
* @param \Closure $next
* @return mixed
*/
public function handle($request, Closure $next)
{
//setting of the locale based on the first segment of the URL
app()->setLocale($request->segment(1));
return $next($request);
}
}
My kernel:
protected $middlewareGroups = [
'web' => [
\App\Http\Middleware\EncryptCookies::class,
\Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse::class,
\Illuminate\Session\Middleware\StartSession::class,
// \Illuminate\Session\Middleware\AuthenticateSession::class,
\Illuminate\View\Middleware\ShareErrorsFromSession::class,
\App\Http\Middleware\VerifyCsrfToken::class,
\Illuminate\Routing\Middleware\SubstituteBindings::class,
\App\Http\Middleware\Localization::class,
],
'api' => [
'throttle:60,1',
\Illuminate\Routing\Middleware\SubstituteBindings::class,
],
];
/**
* The application's route middleware.
*
* These middleware may be assigned to groups or used individually.
*
* @var array
*/
protected $routeMiddleware = [
'auth' => \App\Http\Middleware\Authenticate::class,
'auth.basic' => \Illuminate\Auth\Middleware\AuthenticateWithBasicAuth::class,
'bindings' => \Illuminate\Routing\Middleware\SubstituteBindings::class,
'cache.headers' => \Illuminate\Http\Middleware\SetCacheHeaders::class,
'can' => \Illuminate\Auth\Middleware\Authorize::class,
'guest' => \App\Http\Middleware\RedirectIfAuthenticated::class,
'password.confirm' => \Illuminate\Auth\Middleware\RequirePassword::class,
'signed' => \Illuminate\Routing\Middleware\ValidateSignature::class,
'throttle' => \Illuminate\Routing\Middleware\ThrottleRequests::class,
'verified' => \Illuminate\Auth\Middleware\EnsureEmailIsVerified::class,
'localization' => \App\Http\Middleware\Localization::class,
];