Last active
January 31, 2018 21:39
-
-
Save ercanertan/61962296706446cf0d3a2f07ef058604 to your computer and use it in GitHub Desktop.
Laravel Local
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//Route | |
Route::group(['middleware' => ['web','locale'], 'namespace' => 'Modules\Theme\Http\Controllers'], function() | |
{ | |
Route::post('changelocale', ['as' => 'changelocale', 'uses' => 'HomeController@changeLocale']); | |
} | |
//SetLocale | |
$ php artisan make:middleware SetLocale | |
public function handle($request, Closure $next) | |
{ | |
// dd( Session::has('locale') ); | |
if(!Session::has('locale')) | |
{ | |
Session::put('locale', $request->getPreferredLanguage(['en','fr','tr'])); | |
} | |
app()->setLocale(session('locale')); | |
return $next($request); | |
} | |
//Kernel.php | |
protected $middleware = [ | |
. | |
. | |
. | |
. | |
\App\Http\Middleware\SetLocale::class, | |
]; | |
protected $routeMiddleware = [ | |
. | |
. | |
. | |
'locale' => \App\Http\Middleware\SetLocale::class, | |
]; | |
//Controller | |
use Illuminate\Foundation\Validation\ValidatesRequests; | |
//Trait | |
use ValidatesRequests; | |
public function changeLocale(Request $request) | |
{ | |
// dd($request->locale); | |
$this->validate($request, [ | |
'locale' => 'required|in:fr,en,tr'] | |
); | |
\Session::put('locale', $request->locale); | |
return redirect()->back(); | |
} | |
//View | |
{!! Form::open(['method' => 'POST', 'route' => 'changelocale', 'class' => 'form-inline navbar-select']) !!} | |
<div class="form-group @if($errors->first('locale')) has-error @endif"> | |
<span aria-hidden="true"><i class="fa fa-flag"></i></span> | |
{!! Form::select( | |
'locale', | |
['tr' => 'TR','en' => 'EN', 'fr' => 'FR'], | |
\App::getLocale(), | |
[ | |
'id' => 'locale', | |
'class' => 'form-control', | |
'required' => 'required', | |
'onchange' => 'this.form.submit()', | |
] | |
) !!} | |
<small class="text-danger">{{ $errors->first('locale') }}</small> | |
</div> | |
<div class="btn-group pull-right sr-only"> | |
{!! Form::submit("Change", ['class' => 'btn btn-success']) !!} | |
</div> | |
{!! Form::close() !!} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment