-2

this is my code: the route:

Route::get('lang/change', [LangController::class, 'change'])->name('changeLang');

nav.blade.php

 <div class="col-md-2 col-md-offset-6 text-right">
                <strong>Select Language: </strong>
            </div>
            <div class="col-md-4">
                <select class="form-control changeLang">
                    <option value="en" {{ session()->get('locale') == 'en' ? 'selected' : '' }}>English</option>
                    <option value="ar" {{ session()->get('locale') == 'ar' ? 'selected' : '' }}>Arabic</option>
                </select>
        </div>
          @endauth
        </ul>
      </div>

<span class="text-white">{{ auth()->user() !=null ? auth()->user()->name : "Guest" }}</span>

    </div>
  </nav>
  <script type="text/javascript">

    var url = "{{ route('changeLang') }}";

    $(".changeLang").change(function(){
        window.location.href = url + "?lang="+ $(this).val();
    });

</script>

this is the middleware: (i have registered it in the kernal with web middleware:

public function handle(Request $request, Closure $next)
{
    if (session()->has('locale')) {
        App::setLocale(session()->get('locale'));
    }

    return $next($request);
}
Osama Mohammed
  • 2,433
  • 13
  • 29
  • 61

3 Answers3

0

What is the parameter you're passing. All I see is route-url without params

You can check-out this url if you want or just check this block here from laravel documentation

Occasionally you may need to specify a route parameter that may not always be present in the URI. You may do so by placing a ? mark after the parameter name. Make sure to give the route's corresponding variable a default value:

Route::get('/user/{name?}', function ($name = null) {
    return $name;
});

Route::get('/user/{name?}', function ($name = 'John') {
    return $name;
});

I assume you're going with the latest version of Laravel as well Here is the documentation link for routing with parameters Go To Docs

And here is the exact tab where you can find my snippets Laravel Params

Adnan Siddique
  • 324
  • 3
  • 9
0

In your case there is small misunderstanding.

Your route doesnt accept any of the parameters, you are dealing with request. In you case, the solution should be the next:

    $language = $request->query('lang');
    if (null !== $language) {
        App::setLocale($language);
    }

    return $next($request);
0

finally solved this,now you can get lang params in middleware

web.php

<?php

use Illuminate\Support\Facades\Route;
use App\Http\Middleware\MyMiddleware;

/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| contains the "web" middleware group. Now create something great!
|
*/



Route::middleware([MyMiddleware::class])->group(function () {
    Route::get('lang/change', function () {
        return view('welcome');
    })->name('changeLang');
});

welcome.blade.php

<div class="col-md-2 col-md-offset-6 text-right">
    <strong>Select Language: </strong>
</div>
<div class="col-md-4">
    <select class="form-control changeLang">
        <option value="en" {{ $_GET['lang'] == 'en' ? 'selected' : '' }}>English</option>
        <option value="ar" {{ $_GET['lang'] == 'ar' ? 'selected' : '' }}>Arabic</option>
    </select>
</div>
{{-- @endauth --}}
</ul>
</div>

{{-- <span class="text-white">{{ auth()->user() !=null ? auth()->user()->name : "Guest" }}</span> --}}

</div>
</nav>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js" type="text/javascript"></script>

<script type="text/javascript">

var url = "{{ route('changeLang') }}";

$(".changeLang").change(function(){
window.location.href = url + "?lang="+ $(this).val();
});

</script>

MyMiddleware.php

     <?php

namespace App\Http\Middleware;

use Closure;
use Illuminate\Http\Request;

class MyMiddleware
{
    /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure(\Illuminate\Http\Request): (\Illuminate\Http\Response|\Illuminate\Http\RedirectResponse)  $next
     * @return \Illuminate\Http\Response|\Illuminate\Http\RedirectResponse
     */
    public function handle(Request $request, Closure $next)
    {
        // you can use 
        print_r($_GET);

        // or you can use this also 
        print_r($request->query('lang'));

        // or you can use this also 
        print_r($request->all());
        
        return $next($request);
    }
}