0

I can't use localization prefix because of my client's needs. I'm trying to store locale data into session and group my routes based on that 'locale'. But i cant access session data inside web.php.

$locale = Session::get('locale');

    if($locale == 'tr') {
       Route::get('/kurumsal/{slug}', 'App\Http\Controllers\CorporateController@index')->name('corporate');
    } elseif ($locale == 'eng){
       Route::get('/corporate/{slug}', 'App\Http\Controllers\CorporateController@index')->name('corporate');
    }

LanguageController middleware

class LanguageController extends Controller
{
    public function index($locale = ''){
        if ($locale == '') {
            Session::put('locale', 'eng');
        } else {
            Session::put('locale', $locale);
        }
         return redirect()->back();
    }

}

2 Answers 2

1

You don't need to conditionally set routes if there's no conflicts.

The following works just fine:

Route::get('/kurumsal/{slug}', 'App\Http\Controllers\CorporateController@index')->name('corporate');
Route::get('/corporate/{slug}', 'App\Http\Controllers\CorporateController@index')->name('corporate');

If you want to restrict non-localised routes from being accessed then you do need session access but you can have that via middleware e.g.:

class LocaleRestricted {

     public function handle($request, $next, ...$localesAllowed) {
          if (!in_array(Session::get('locale'), $localesAllowed)) {
              abort(404, 'This route is not available for this locale');
          }
          return $next($request);  
     }
}

You then need to register this locale in your app/Http/Kernel.php:

// ...
protected $routeMiddleware = [
   // ...
   'locale_restricted' => LocaleRestricted::class
];

Then you can use it as:

$locale = Session::get('locale');

Route::middleware('locale_restricted:tr')->group(function () {
       Route::get('/kurumsal/{slug}', 'App\Http\Controllers\CorporateController@index')->name('corporate');
});
Route::middleware('locale_restricted:eng')->group(function () {

       Route::get('/corporate/{slug}', 'App\Http\Controllers\CorporateController@index')->name('corporate');
});

Sign up to request clarification or add additional context in comments.

2 Comments

Well that was much easier than i thought. By the way, is there any way to setLocale based on my routes ? Because if anyone just go for siteurl.com/corporate he'll see Turkish translations because default locale is set to 'tr'.
You can use the above solution with the middleware but do setLocale instead of doing the abort (note that $locale is an array so you'd probably need to use $locale[0]??'default locale value' (where default locale value is what you want the default to be in case you ever use the middleware without specifying it)
1

For Get Data from session you can use

$value = $request->session()->get('your-key-name');

But I'm not sure It works in Web.php or not..!! But you can use this one in Controller, Middelware etc many more places.

For More Details See Laravel Official Documentation

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.