我在\routes\web.php中使用Request:: path (),如下所示,访问'/home/test/test1‘运行良好,即路径显示在网站上。
<?php
use Illuminate\Support\Facades\Route;
Route::get('/', function () {
return view('welcome');
});
Route::get('/home/test/test1', function(){
echo Request::path();
});但是,当我在\app\Http\Middleware中执行此操作时,出现错误"Class 'App\Http\Middleware\Request‘not found“。
<?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);
}
}发布于 2020-04-03 19:27:42
您在您的中间件文件中遗漏了:
use Illuminate\Http\Request;将您的中间件代码更改为:
<?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);
}
}这应该可以解决这个问题。
参考:https://laravel.com/docs/5.8/requests#request-path-and-method
发布于 2020-04-03 19:54:14
实际上,您没有在中间件文件中添加引用。因此,在名称空间下面添加use Illuminate\Http\Request;。那么错误应该被修复。
https://stackoverflow.com/questions/61010809
复制相似问题