Laravel/PHP:您访问的页面不存在,

qmb5sa22  于 12个月前  发布在  PHP
关注(0)|答案(4)|浏览(97)

我在一些API调用上遇到了问题。我能够通过使用laravel cors by fruitcake绕过这个初始问题-
对预检请求的响应未通过访问控制检查
但问题并没有就此停止,一些API调用抛出了此错误
“错误”:“类cors不存在”,“调试”:{“代码”:-1,“行”:767,“跟踪”:[{“文件”:“/home/xxxxxx/Workspace/xxxxxx/vendor/laravel/framework/src/Illuminate/Container/Container.php”,“行”:767.]}
我能够遵循fruitcake/laravel-cors的指令,包括将其添加到kernel.php中,创建paths/cors.php并将路径设置为'paths' => ['api/*'],,最后将其添加到我的路由中:

Route::group(['middleware' => 'cors'], function() {
    Route::middleware('auth:api')->namespace('Api')->group(function () {

     
        Route::middleware('admin')->prefix('admin')->namespace('Admin')->group(function() {
            Route::get('dashboard/statistics', 'DashboardController@index');

...

字符串
我使用的是旧版本的Laravel(5.6)和PHP 7.4。如果您能提供帮助,我将不胜感激!
当我在新选项卡上打开端点时,出现错误:
Symfony \ Component \ HttpKernel \ Exception \ MethodNotAllowedHttpException No message

8fq7wneg

8fq7wneg1#

HandleCors类添加到config/app.php别名部分,如下所示。

'cors' => \Fruitcake\Cors\HandleCors::class,

字符串

k7fdbhmy

k7fdbhmy2#

在将HandleCors添加到config/app.php后,是否运行了composer dump-autoload命令?

pkwftd7m

pkwftd7m3#

我删除了路由器上的中间件

Route::group(['middleware' => 'cors'], function() {
    Route::middleware('auth:api')->namespace('Api')->group(function () {

 
        Route::middleware('admin')->prefix('admin')->namespace('Admin')->group(function() {
            Route::get('dashboard/statistics', 'DashboardController@index');

...

字符串
只是

Route::middleware('admin')->prefix('admin')->namespace('Admin')->group(function() {
    Route::get('dashboard/statistics', 'DashboardController@index');

sycxhyv7

sycxhyv74#

这可能发生在升级laravel应用程序之后。
您必须将App\Http\Kernel.php的中间件数组中的**\Fruitcake\Cors\HandleCors::class**替换为

\Illuminate\Http\Middleware\HandleCors::class,

字符串
现在将是:

protected $middleware = [
        // \App\Http\Middleware\TrustHosts::class,
        \App\Http\Middleware\TrustProxies::class,
        \Illuminate\Http\Middleware\HandleCors::class,
        \App\Http\Middleware\PreventRequestsDuringMaintenance::class,
        \Illuminate\Foundation\Http\Middleware\ValidatePostSize::class,
        \App\Http\Middleware\TrimStrings::class,
        \Illuminate\Foundation\Http\Middleware\ConvertEmptyStringsToNull::class,
    ];

相关问题