Ad

Request A Url In Middleware But Get Class 'App\Http\Middleware\Request' Not Found

- 1 answer

I use Request::path() in \routes\web.php as follow and visit '/home/test/test1' worked well, i.e., the path is shown in the website.

<?php

use Illuminate\Support\Facades\Route;

Route::get('/', function () {
    return view('welcome');
});

Route::get('/home/test/test1', function(){
    echo Request::path();
});

However, when I did it in \app\Http\Middleware, the error "Class 'App\Http\Middleware\Request' not found" showed up.

<?php

namespace App\Http\Middleware;

use Closure;

class CheckAuth
{
    /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure  $next
     * @return mixed
     */
    public function handle($request, Closure $next)
    {
        $path = Request::path();
        return $next($request);
    }
}
Ad

Answer

You missed in your Middleware file:

use Illuminate\Http\Request;

Change your Middleware code to:

<?php

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


class CheckAuth
{
    /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure  $next
     * @return mixed
     */
    public function handle(Request $request, Closure $next)
    {
        $path = $request->path();
        return $next($request);
    }
}

That should fix it.

Reference: https://laravel.com/docs/5.8/requests#request-path-and-method

Ad
source: stackoverflow.com
Ad