我的一些 Laravel 路由(PUT 和 DELETE)在共享主机上不起作用

Posted

技术标签:

【中文标题】我的一些 Laravel 路由(PUT 和 DELETE)在共享主机上不起作用【英文标题】:Some of my Laravel routes (PUT and DELETE) doesn't work on a shared hosting 【发布时间】:2015-04-17 00:04:52 【问题描述】:

我用 Laravel 开发了一个 Web 应用程序。在我的本地开发环境和专用服务器上一切都很好。但是当我切换到共享主机 (OVH) 时,当我尝试更新或删除资源时,Laravel 会返回一个 MethodNotAllowedHttpException。 HTTP GETPOST 请求效果很好。

我不知道我的主机是否允许GETPOST 以外的请求。但实际上问题不存在(我认为):因为 Laravel 使用 POST 请求来“模拟”PUTDELETE 请求(使用隐藏类型的输入“_method”)。所以我认为这不是问题。

我需要您的帮助来找出问题所在。 我不明白为什么我的路线适用于开发环境并且(我的某些路线)不适用于我的共享主机。

这没有意义!

您是否需要其他信息(可能是 php.ini?)


编辑

我的 routes.php 文件(这些路由可以使用 WAMP 或在我的专用服务器上):

// Backend
Route::group(['prefix' => 'admin', 'namespace' => 'Controllers\Backend', 'before' => 'auth.wordpress'], function() 

    Route::get('/', 'DashboardController@index');

    Route::get('products/status', ['as' => 'admin.products.index.status', 'uses' => 'ProductsController@index'])->where('status', 'publish|draft|featured');
    Route::post('products/index', ['as' => 'admin.products.filter', 'uses' => 'ProductsController@index']);
    Route::resource('products', 'ProductsController');

    Route::post('categories/index', ['as' => 'admin.categories.filter', 'uses' => 'CategoriesController@index']);
    Route::resource('categories', 'CategoriesController');

    Route::post('manufacturers/index', ['as' => 'admin.manufacturers.filter', 'uses' => 'ManufacturersController@index']);
    Route::resource('manufacturers', 'ManufacturersController');

    Route::post('attributes/index', ['as' => 'admin.attributes.filter', 'uses' => 'AttributesController@index']);
    Route::resource('attributes', 'AttributesController');

    Route::resource('upload', 'UploadController', ['only' => array('store', 'destroy')]);
    Route::post('upload/from', ['as' => 'admin.upload.store.from', 'uses' => 'UploadController@store']);

);

路由php artisan routes 命令生成:

+--------+---------------------------------------------------+-------------------------------------+-----------------------------------------------------------+----------------+---------------+
| Domain | URI                                               | Name                                | Action                                                    | Before Filters | After Filters |
+--------+---------------------------------------------------+-------------------------------------+-----------------------------------------------------------+----------------+---------------+
|        | GET|HEAD /                                        |                                     | Closure                                                   |                |               |
|        | GET|HEAD products/featured                        | frontend.products.featured          | Controllers\Frontend\ProductsController@featured          |                |               |
|        | GET|HEAD products/sidebar/slugCategory?         | frontend.products.sidebar           | Controllers\Frontend\ProductsController@sidebar           |                |               |
|        | GET|HEAD products/last                            | frontend.products.last              | Controllers\Frontend\ProductsController@last              |                |               |
|        | POST products/search/page?                      | frontend.products.search            | Controllers\Frontend\ProductsController@search            |                |               |
|        | GET|HEAD products/show/slug                     | frontend.products.show              | Controllers\Frontend\ProductsController@show              |                |               |
|        | GET|HEAD products/show/slug/meta                | frontend.products.show.meta         | Controllers\Frontend\ProductsController@showMeta          |                |               |
|        | GET|POST|HEAD products/category?/page?        | frontend.products.show              | Controllers\Frontend\ProductsController@index             |                |               |
|        | GET|HEAD categories/show/category/meta          | frontend.categories.meta            | Controllers\Frontend\ProductsController@categoryMeta      |                |               |
|        | GET|HEAD manufacturers/listHomepage               | frontend.manufacturers.listHomepage | Controllers\Frontend\ManufacturersController@listHomepage |                |               |
|        | GET|HEAD manufacturers                            | frontend.manufacturers.index        | Controllers\Frontend\ManufacturersController@index        |                |               |
|        | GET|HEAD manufacturers/slug                     | frontend.manufacturers.show         | Controllers\Frontend\ManufacturersController@show         |                |               |
|        | GET|HEAD manufacturers/slug/meta                | frontend.manufacturers.show.meta    | Controllers\Frontend\ManufacturersController@showMeta     |                |               |
|        | GET|HEAD admin                                    |                                     | Controllers\Backend\DashboardController@index             | auth.wordpress |               |
|        | GET|HEAD admin/products/status                  | admin.products.index.status         | Controllers\Backend\ProductsController@index              | auth.wordpress |               |
|        | POST admin/products/index                         | admin.products.filter               | Controllers\Backend\ProductsController@index              | auth.wordpress |               |
|        | GET|HEAD admin/products                           | admin.products.index                | Controllers\Backend\ProductsController@index              | auth.wordpress |               |
|        | GET|HEAD admin/products/create                    | admin.products.create               | Controllers\Backend\ProductsController@create             | auth.wordpress |               |
|        | POST admin/products                               | admin.products.store                | Controllers\Backend\ProductsController@store              | auth.wordpress |               |
|        | GET|HEAD admin/products/products                | admin.products.show                 | Controllers\Backend\ProductsController@show               | auth.wordpress |               |
|        | GET|HEAD admin/products/products/edit           | admin.products.edit                 | Controllers\Backend\ProductsController@edit               | auth.wordpress |               |
|        | PUT admin/products/products                     | admin.products.update               | Controllers\Backend\ProductsController@update             | auth.wordpress |               |
|        | PATCH admin/products/products                   |                                     | Controllers\Backend\ProductsController@update             | auth.wordpress |               |
|        | DELETE admin/products/products                  | admin.products.destroy              | Controllers\Backend\ProductsController@destroy            | auth.wordpress |               |
|        | POST admin/categories/index                       | admin.categories.filter             | Controllers\Backend\CategoriesController@index            | auth.wordpress |               |
|        | GET|HEAD admin/categories                         | admin.categories.index              | Controllers\Backend\CategoriesController@index            | auth.wordpress |               |
|        | GET|HEAD admin/categories/create                  | admin.categories.create             | Controllers\Backend\CategoriesController@create           | auth.wordpress |               |
|        | POST admin/categories                             | admin.categories.store              | Controllers\Backend\CategoriesController@store            | auth.wordpress |               |
|        | GET|HEAD admin/categories/categories            | admin.categories.show               | Controllers\Backend\CategoriesController@show             | auth.wordpress |               |
|        | GET|HEAD admin/categories/categories/edit       | admin.categories.edit               | Controllers\Backend\CategoriesController@edit             | auth.wordpress |               |
|        | PUT admin/categories/categories                 | admin.categories.update             | Controllers\Backend\CategoriesController@update           | auth.wordpress |               |
|        | PATCH admin/categories/categories               |                                     | Controllers\Backend\CategoriesController@update           | auth.wordpress |               |
|        | DELETE admin/categories/categories              | admin.categories.destroy            | Controllers\Backend\CategoriesController@destroy          | auth.wordpress |               |
|        | POST admin/manufacturers/index                    | admin.manufacturers.filter          | Controllers\Backend\ManufacturersController@index         | auth.wordpress |               |
|        | GET|HEAD admin/manufacturers                      | admin.manufacturers.index           | Controllers\Backend\ManufacturersController@index         | auth.wordpress |               |
|        | GET|HEAD admin/manufacturers/create               | admin.manufacturers.create          | Controllers\Backend\ManufacturersController@create        | auth.wordpress |               |
|        | POST admin/manufacturers                          | admin.manufacturers.store           | Controllers\Backend\ManufacturersController@store         | auth.wordpress |               |
|        | GET|HEAD admin/manufacturers/manufacturers      | admin.manufacturers.show            | Controllers\Backend\ManufacturersController@show          | auth.wordpress |               |
|        | GET|HEAD admin/manufacturers/manufacturers/edit | admin.manufacturers.edit            | Controllers\Backend\ManufacturersController@edit          | auth.wordpress |               |
|        | PUT admin/manufacturers/manufacturers           | admin.manufacturers.update          | Controllers\Backend\ManufacturersController@update        | auth.wordpress |               |
|        | PATCH admin/manufacturers/manufacturers         |                                     | Controllers\Backend\ManufacturersController@update        | auth.wordpress |               |
|        | DELETE admin/manufacturers/manufacturers        | admin.manufacturers.destroy         | Controllers\Backend\ManufacturersController@destroy       | auth.wordpress |               |
|        | POST admin/attributes/index                       | admin.attributes.filter             | Controllers\Backend\AttributesController@index            | auth.wordpress |               |
|        | GET|HEAD admin/attributes                         | admin.attributes.index              | Controllers\Backend\AttributesController@index            | auth.wordpress |               |
|        | GET|HEAD admin/attributes/create                  | admin.attributes.create             | Controllers\Backend\AttributesController@create           | auth.wordpress |               |
|        | POST admin/attributes                             | admin.attributes.store              | Controllers\Backend\AttributesController@store            | auth.wordpress |               |
|        | GET|HEAD admin/attributes/attributes            | admin.attributes.show               | Controllers\Backend\AttributesController@show             | auth.wordpress |               |
|        | GET|HEAD admin/attributes/attributes/edit       | admin.attributes.edit               | Controllers\Backend\AttributesController@edit             | auth.wordpress |               |
|        | PUT admin/attributes/attributes                 | admin.attributes.update             | Controllers\Backend\AttributesController@update           | auth.wordpress |               |
|        | PATCH admin/attributes/attributes               |                                     | Controllers\Backend\AttributesController@update           | auth.wordpress |               |
|        | DELETE admin/attributes/attributes              | admin.attributes.destroy            | Controllers\Backend\AttributesController@destroy          | auth.wordpress |               |
|        | POST admin/upload                                 | admin.upload.store                  | Controllers\Backend\UploadController@store                | auth.wordpress |               |
|        | DELETE admin/upload/upload                      | admin.upload.destroy                | Controllers\Backend\UploadController@destroy              | auth.wordpress |               |
|        | POST admin/upload/from                          | admin.upload.store.from             | Controllers\Backend\UploadController@store                | auth.wordpress |               |
+--------+---------------------------------------------------+-------------------------------------+-----------------------------------------------------------+----------------+---------------+

phpinfo(): https://www.dropbox.com/s/mj7d6c6ptdj9i4u/phpinfo_ovh.png?dl=0

错误的堆栈跟踪:

Symfony\Component\HttpKernel\Exception\MethodNotAllowedHttpException thrown with message ""

Stacktrace:
#13 Symfony\Component\HttpKernel\Exception\MethodNotAllowedHttpException in /home/guicara/vendor/laravel/framework/src/Illuminate/Routing/RouteCollection.php:210
#12 Illuminate\Routing\RouteCollection:methodNotAllowed in /home/guicara/vendor/laravel/framework/src/Illuminate/Routing/RouteCollection.php:197
#11 Illuminate\Routing\RouteCollection:getOtherMethodsRoute in /home/guicara/vendor/laravel/framework/src/Illuminate/Routing/RouteCollection.php:145
#10 Illuminate\Routing\RouteCollection:match in /home/guicara/vendor/laravel/framework/src/Illuminate/Routing/Router.php:1049
#9 Illuminate\Routing\Router:findRoute in /home/guicara/vendor/laravel/framework/src/Illuminate/Routing/Router.php:1017
#8 Illuminate\Routing\Router:dispatchToRoute in /home/guicara/vendor/laravel/framework/src/Illuminate/Routing/Router.php:996
#7 Illuminate\Routing\Router:dispatch in /home/guicara/vendor/laravel/framework/src/Illuminate/Foundation/Application.php:775
#6 Illuminate\Foundation\Application:dispatch in /home/guicara/vendor/laravel/framework/src/Illuminate/Foundation/Application.php:745
#5 Illuminate\Foundation\Application:handle in /home/guicara/vendor/laravel/framework/src/Illuminate/Session/Middleware.php:72
#4 Illuminate\Session\Middleware:handle in /home/guicara/vendor/laravel/framework/src/Illuminate/Cookie/Queue.php:47
#3 Illuminate\Cookie\Queue:handle in /home/guicara/vendor/laravel/framework/src/Illuminate/Cookie/Guard.php:51
#2 Illuminate\Cookie\Guard:handle in /home/guicara/vendor/stack/builder/src/Stack/StackedHttpKernel.php:23
#1 Stack\StackedHttpKernel:handle in /home/guicara/vendor/laravel/framework/src/Illuminate/Foundation/Application.php:641
#0 Illuminate\Foundation\Application:run in /home/guicara/www/public/index.php:61

编辑 2

共享主机 (OVH) 上的 Firebug 调试:

在 WAMP(本地)上进行 Firebug 调试...对于相同的请求(更新产品):

【问题讨论】:

如果 Laravel 使用隐藏字段模拟 PUT 和 DELETE,我认为主机不会有问题。如果它真的在执行 PUT 或 DELETE,那么很可能是主机阻止了这些请求。 尝试将 / 放在第一个字母处,而不是仅在 routes.php 文件中的 /products 等路由中命名 @kamlesh.bar 不幸的是,这不是解决方案(添加斜线时未定义路由 X) 【参考方案1】:

我认为很清楚发生了什么。 您为 POSTGET 设置了路由,但希望 DELETEPUT 能够神奇地执行路由。 p>

Route::delete('attributes/destroy/id','AttributesController@destroy');
Route::put('products/id', 'ProductsController@update');

最好的方法是隐式路由所有内容。

【讨论】:

使用Route::resource(...)GETPOSTDELETEPUT 设置路由。使用Route::resource,我不需要手动编写 Route::get ... 等。此外,我的路由在本地主机上也可以! php artisan routes 输出已添加(见上表) 但是你试过了吗?根据我对 laravel 的经验,当您添加“帖子”添加时,有时资源会被覆盖。 我只尝试了一条路线Route::resource('products', 'ProductsController');MethodNotAllowedHttpException 总是在这里! 反过来呢?在没有资源的情况下写出所有可能的方法?在 laravel4 中,我删除了所有资源,这让我继续前进。

以上是关于我的一些 Laravel 路由(PUT 和 DELETE)在共享主机上不起作用的主要内容,如果未能解决你的问题,请参考以下文章

Laravel CSRF 令牌不匹配异常。通过 jQuery ajax 向资源路由发送“Put”请求

此路由不支持 PUT 方法。支持的方法:GET、HEAD、POST。在 laravel 中

laravel 不支持 put 方法

Laravel Put Route Giving 404 错误

laravel向我显示此错误此路由不支持POST方法。支持的方法:GET,HEAD,PUT,DELETE

Laravel 6.0 不支持 DELETE、PUT 方法