【问题标题】:Laravel localization package throws MethodNotAllowedHttpException on POST requestLaravel 本地化包在 POST 请求时抛出 MethodNotAllowedHttpException
【发布时间】:2017-08-10 10:56:39
【问题描述】:

我使用这个包进行本地化https://github.com/mcamara/laravel-localization,它在大多数情况下都可以正常工作,除了一个 POST 请求。

当我尝试访问时:

Route::group(
    [
        'prefix'     => LaravelLocalization::setLocale(),
        'middleware' => ['localeSessionRedirect', 'localizationRedirect', 'auth'],
    ],
    function () {
    Route::post('bsSearch', 'BrandSpendingsController@search')->middleware('premium');
}

我得到一个MethodNotAllowedHttpException ...当我将它切换到GET 时,它工作正常......但是它适用于英语。如果我将 url 切换为http://localhost/de/bsSearch,则会引发错误。

Route::get('bsSearch', 'BrandSpendingsController@search')->middleware('premium');

我真的不知道为什么,因为这个工作正常而且它也是一个POST 方法:

Route::post('editUserProfile/{id}', 'UserController@edit');

我使用 Laravel 5.3。

编辑

英文网址有效,因为我在laravellocalization.php 中设置了'hideDefaultLocaleInURL' => true,,并且它隐藏了网址中的“en”前缀。如果我将它设置为false,它也会抛出英语错误。

【问题讨论】:

  • 尽量使用 PUT 方法而不是 POST,不要忘记更改表单中的方法类型
  • 不工作 :(.
  • 你改变路线了吗?像这样:Route::put('editUserProfile/{id}', 'UserController@edit');
  • 是的。并且 editUserProfile 可以作为 POST 请求,但 bsSearch 不能。
  • 我在插件 mcamara/laravel-localization 中使用除 GET 之外的任何方法都有同样的问题。使用 GET 一切正常,但使用 POST、PUT、DELETE 它重定向到 GET

标签: php laravel


【解决方案1】:

Here 是一个答案

  1. 最好只本地化 GET 路由,将它们放到本地化组中,不要放其他路由
  2. 如果您需要本地化其他方法,例如 POST、PUT、PATCH、DELETE,您应该按照here 的描述本地化您的 url

【讨论】:

    猜你喜欢
    • 2013-11-14
    • 2018-05-28
    • 2014-09-16
    • 2014-07-30
    • 2016-04-23
    • 1970-01-01
    • 2021-12-31
    • 1970-01-01
    • 2019-05-07
    相关资源
    最近更新 更多