Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
491 views
in Technique[技术] by (71.8m points)

Too few arguments to function AppHttpMiddlewareLocale::handle() | Laravel 8

when I try to insert the third parameter in the handle method, I get the following error:

'Too few arguments to function AppHttpMiddlewareLocale::handle(), 2 passed in /Applications/MAMP/htdocs/weather-app/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php on line 167 and exactly 3 expected'

How can I fix it?

My middleware:

namespace AppHttpMiddleware;

use AppServicesSettingsService;
use Closure;
use IlluminateHttpRequest;

class Locale
{
    /**
     * Handle an incoming request.
     *
     * @param  IlluminateHttpRequest  $request
     * @param  Closure  $next
     * @param  SettingsService $settingsService
     * @return mixed
     */
    public function handle(Request $request, Closure $next, SettingsService $settingsService): mixed
    {
        $locale = $settingsService->get('language', app()->getLocale());
        app()->setLocale($locale);

        return $next($request);
    }
}

Kernel:

'web' => [
            AppHttpMiddlewareEncryptCookies::class,
            IlluminateCookieMiddlewareAddQueuedCookiesToResponse::class,
            IlluminateSessionMiddlewareStartSession::class,
            // IlluminateSessionMiddlewareAuthenticateSession::class,
            IlluminateViewMiddlewareShareErrorsFromSession::class,
            AppHttpMiddlewareVerifyCsrfToken::class,
            IlluminateRoutingMiddlewareSubstituteBindings::class,
            AppHttpMiddlewareLocale::class,
        ],
question from:https://stackoverflow.com/questions/65853513/too-few-arguments-to-function-app-http-middleware-localehandle-laravel-8

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Inject the SettingsService $settingsService into your __construct().

class Locale
{
  private SettingsService $settingsService;

  public function __construct(SettingsService $settingsService)
  {
    $this->settingsService = $settingsService;
  }

  public function handle(Request $request, Closure $next): mixed
  {
    // access via $this->settingsService
  }
}

The handle method only accepts two parameters. So use the constructor injection to solve this problem.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...