I'm rewriting my app in terms of localization. I had it all setup and it worked, but I decided to use a middleware so I can control which routes should be localized and which not.
I'm having a problem with links generated on my page. All of the links are generated as the following
www.example.com/index
While the correct link would be
www.example.com/en/index
I have prefixed all my routes the following way
Route::group(['prefix' => '{lang}', 'middleware' => 'localize'], function () {
And my middleware localization class is the following:
<?php
namespace App\Http\Middleware;
use Closure;
use Request;
class Localize
{
/**
* Handle an incoming request.
*
* @param \Illuminate\Http\Request $request
* @param \Closure $next
* @return mixed
*/
public function handle($request, Closure $next)
{
$locale = $request->lang;
if(!array_key_exists($locale, config('app.locales')) && Request::path() != '/') {
abort(404);
}
\App::setLocale($locale);
return $next($request);
}
}
In my Blade view, I am using
to generate links to different parts of my website (all routes are named). I know I can circumvent this issue by using
App::getLocale()
to prefix all my URLs in my blade template, but this is not a valid solution for me as it would probably introduce human errors (copy pasting the same line, there's a good chance I might mess something up) and it is time consuming. Is there a better way to achieve this?
via Petar-Krešimir