4

My project is multi language, so in most route I add parameter "locale". But latter I do not need to pass "locale" parameter to my controllers. I try to remove this parameter using forgetParameter() method but when I use this method then I have error "Route not bound".

So what I'm doing wrong.

How to remove "locale" from all routes?

Laravel 5.8

My route file.

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

Auth::routes([app()->getLocale()]);
Route::get('/', 'HomeController@index')->name('mainPage');


Route::get('/user/{id}','UserController@showUser')->name('getUserProfile')->forgetParameter('locale');
Route::get('/user/{id}/edit','UserController@editUser')->name('editUserProfile')
    ->middleware('can:editUser,App\User,id')->forgetParameter('locale');
Route::patch('/user/{id}/edit','UserController@updateUser')->name('updateUserProfile')
    ->middleware('can:updateUser,App\User,id')->forgetParameter('locale');

});

It's not duplicate with this question - Laravel 5.4: How to DO NOT use route parameter in controller

Solution in that question doesn't work in my case. And my question is why "forgotParamter()" throw an error?

Other question is, why I can't use this construction in middleware:

$request->route()->forgetParameter('locale')

I have following error:

Call to a member function is() on null

Thank you.

Klick
  • 1,418
  • 1
  • 23
  • 45
  • Possible duplicate of [Laravel 5.4: How to DO NOT use route parameter in controller](https://stackoverflow.com/questions/44619512/laravel-5-4-how-to-do-not-use-route-parameter-in-controller) – nakov Jun 21 '19 at 05:40
  • I had a similar issue. Is it possible that you're attempting to use a global middleware? According to this,[$request->route() is null in Middleware, how can I filter by route parameters?](https://stackoverflow.com/questions/31868759/request-route-is-null-in-middleware-how-can-i-filter-by-route-parameters) the route can't be accessed in globals, so you would need to register it as a route middleware. – Brec Aug 04 '20 at 15:36

1 Answers1

0

Try to forget the parameter in an inline middleware in the controller's constructor. Actually, I would use a base controller: 1, get the route value; 2, set to a protected property, so inherited controllers can access to the value; 3, forget the parameter.

<?php
// Localized/Controller.php
namespace App\Http\Controllers\Localized;

use App\Http\Controllers\Controller as ControllerBase;
use Illuminate\Http\Request;

class Controller extends ControllerBase
{
    protected string $currentLocale;

    public function __construct()
    {
        $this->middleware(function ($request, $next) {
            // If you need to access this later on inherited controllers
            $this->currentLocale = $request->route('locale') ?? 'en';

            // Other operations, like setting the locale or check if lang is available, etc
 
            $request->route()->forgetParameter('locale');

            return $next($request);
        });
    }
}

<?php
// Localized/UserController.php
namespace App\Http\Controllers\Localized;

use Illuminate\Http\Request;

// Extends the our base controller instead of App\Http\Controllers\Controller
class UserController extends Controller 
{
    public function show(Request $request)
    {
        // No need of string $locale arg
        dd($this->currentLocale);
    }

}
nelson6e65
  • 917
  • 11
  • 14