laravellaravel-localization

laravel localization set the default locale parameter replacing other parameters


I am new to laravel. when I set the default locale from localization.php and I click on any of my data list edit buttons it replaces the id variable to locale value like (en/ ur/ sd).

then I remove the default locale parameter from Localization.php

ill get Missing parameter: about

web.php

Route::group([
    'prefix' => '{locale}', 
    'where' => ['locale' => '[a-zA-Z]{2}'], 
    'middleware' => 'Localization'
    ], function() {


        Route::get('/', function () {
            return redirect(route('home'));
        });
        
Route::resource('/about', AboutController::class)->middleware('auth');
});

kernel.php

protected $routeMiddleware = [
        'auth' => \App\Http\Middleware\Authenticate::class,
        'auth.basic' => \Illuminate\Auth\Middleware\AuthenticateWithBasicAuth::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,

        // Permission
        'role' => \Spatie\Permission\Middlewares\RoleMiddleware::class,
        'permission' => \Spatie\Permission\Middlewares\PermissionMiddleware::class,
        'role_or_permission' => \Spatie\Permission\Middlewares\RoleOrPermissionMiddleware::class,
        'Localization' => \App\Http\Middleware\Localization::class,
    ];

Localization.php

class Localization
{
    /**
    * Handle an incoming request.
    *
    * @param  \Illuminate\Http\Request  $request
    * @param  \Closure  $next
    * @return mixed
    */
    public function handle(Request $request, Closure $next)
    {        

        URL::defaults(['locale' => $request->segment(1)]);
        app()->setLocale($request->segment(1));
        return $next($request);
        
        
    }
}

AboutController.php edit function

public function edit($id)
    {
       
        $singleRow = About::find($id); 
        $allRows = About::all();
        return view('about', compact('allRows','singleRow'));
    }

app.php

'locale' => 'en',
'fallback_locale' => 'en',

    'available_locales' => [
         'en',
         'sd',
         'ur',
      ],

about.blade.php Edit section when I click here for edit data

if I try without app()->getLocale() and the locale parameter is assigned from Localization.php I ll replace the id as selected locale language like en, sd, ur whatever is given in URL like http://127.0.0.1:8000/en/about/1/edit

1st try with default locale parameter mentioned above

<a href="{{ route('about.edit', ['about' => $about->id]) }}" class="btn btn-primary">Edit</a> 

second try without locale parameter mentioned above

<a href="{{ route('about.edit', ['locale'=> app()->getLocale(), 'about' => $about->id]) }}" class="btn btn-primary">Edit</a> 

in this time I have set the default locale from Localization.php mentioned above and removed the locale parameter from my tag

<a href="{{ route('about.edit', ['about' => $about->id]) }}" class="btn btn-primary">Edit</a> 

I get this in my browser when I clicked on edit button

changed return to dd

public function edit($id)
    {
       
        dd($id);
        $singleRow = About::find($id); 
        $allRows = About::all();
        return view('about', compact('allRows','singleRow'));

    }

image is here

in this time I commented on the locale parameter from Localization.php and added the locale parameter in my tag

<a href="{{ route('about.edit', ['locale'=> app()->getLocale(), 'about' => $about->id]) }}" class="btn btn-primary">Edit</a>

enter image description here


Solution

  • Localization.php

    class Localization
        {
            /**
            * Handle an incoming request.
            *
            * @param  \Illuminate\Http\Request  $request
            * @param  \Closure  $next
            * @return mixed
            */
            public function handle(Request $request, Closure $next)
            {        
        
                URL::defaults(['locale' => $request->segment(1)]);
                app()->setLocale($request->segment(1));
                return $next($request);
                
                
            }
        }
    

    language switcher where i was getting the error. in master.blade.php

    @foreach (config('app.available_locales') as $locale)
                <li class="dropdown messages-menu">
                    <a class="nav-link"
                    href="{{ route(\Illuminate\Support\Facades\Route::currentRouteName(), ['locale'=>$locale,0]) }}"
                        @if (app()->getLocale() == $locale) style="font-weight: bold; text-decoration: underline" @endif>{{ strtoupper($locale) }}</a>
                </li>
            @endforeach
    

    here in master.blade.php i set $locale with ['locale'=> $locale, 0] i don't know second parameter is why required but its working fine

    and i have changed the controller edit function as

    public function edit($ignore='', $id)
        {
           
            $singleRow = About::find($id); 
            $allRows = About::all();
            return view('about', compact('allRows','singleRow'));
        }
    

    here i am getting the id when i click on the edit

    <a href="{{ route('about.edit', ['about' => $about->id]) }}" class="btn btn-primary">Edit</a>