Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Argument 1 passed to Illuminate\Routing\Middleware\ThrottleRequests::addHeaders() must be an instance of

Tags:

php

laravel

I have Created a new middleware for checking the user token I have create middleware then adeded to kernal.php and but when i tried to access $request in middleware i am getting the error

Here is my is my middleware code

namespace App\Http\Middleware;
use Illuminate\Http\Request;
use Closure;
use App\User;

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

        if($request->header('Token') == '123')
        {
            return ['status' =>2, 'msg' => 'Unathorized'];
        }
        else
        {
            return $next($request);
         }

    }
}

Here is my kernal file

 protected $routeMiddleware = [
        'auth' => \App\Http\Middleware\Authenticate::class,
        'auth.basic' => \Illuminate\Auth\Middleware\AuthenticateWithBasicAuth::class,
        'bindings' => \Illuminate\Routing\Middleware\SubstituteBindings::class,
        'cache.headers' => \Illuminate\Http\Middleware\SetCacheHeaders::class,
        'can' => \Illuminate\Auth\Middleware\Authorize::class,
        'guest' => \App\Http\Middleware\RedirectIfAuthenticated::class,
        'signed' => \Illuminate\Routing\Middleware\ValidateSignature::class,
        'throttle' => \Illuminate\Routing\Middleware\ThrottleRequests::class,
        'verified' => \Illuminate\Auth\Middleware\EnsureEmailIsVerified::class,
        'verifyToken' => \App\Http\Middleware\CheckToken::class,
    ];

and I am getting this error

Argument 1 passed to Illuminate\Routing\Middleware\ThrottleRequests::addHeaders() must be an instance of Symfony\Component\HttpFoundation\Response, instance of Illuminate\Http\Request given, called in C:\xampp\htdocs\idoltime\vendor\laravel\framework\src\Illuminate\Routing\Middleware\ThrottleRequests.php on line 62
like image 491
sulay panchal Avatar asked Jan 26 '23 08:01

sulay panchal


1 Answers

You may use

// return $request;
if($request->header('Token') == '123')
{
        $response = [
            'status' => 2,
            'message' => 'Unauthorized',
        ];

        return response()->json($response, 413);
}
else
{
        return $next($request);
}
like image 77
Foued MOUSSI Avatar answered Jan 27 '23 21:01

Foued MOUSSI