【问题标题】:Exception with message `No message` on posting data from a form从表单发布数据时出现消息“无消息”的异常
【发布时间】:2023-03-16 08:04:01
【问题描述】:

访问路由器时出现此错误:

Symfony\Component\HttpKernel\Exception\ MethodNotAllowedHttpException 无消息

我试图找出它,但我无法解决它。

路由器(web.php)

Route::get('/test', function ()
{
    return view('subdomains.account.pages.test');
});
Route::post('/testForm', 'FormController@store');

查看(子域.account.pages.test.blade.php)

{!! Form::open(['action' => [ 'FormController@store' ], 'method' => 'POST']) !!}
    {!! Form::submit('test') !!}
{!! Form::close(); !!}

控制器

namespace Ares\Http\Controllers;

use Illuminate\Http\Request;

class FormController extends Controller
{
    public function store()
    {
        return "test";
    }
}

问题是代码总是使用 GET 方法而不是 POST。 我该如何解决?

编辑:我刚刚发现这个问题是因为我没有使用 php artisan serve 使用 Laravel,而且我只有一个 虚拟主机公共服务器 中,如何在不使用 artisan serve 的情况下解决这个问题?

修复:(已编辑)

问题是在 URL 的末尾强制使用斜杠。

【问题讨论】:

  • 试试php artisan route:clear
  • 仍在工作。

标签: laravel


【解决方案1】:

试试这个:

FormController.php

<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
class FormController extends Controller
{
  public function store()
  {
      return "test";
  }
}

子域/帐户/页面/test.blade.php

{!! Form::open(['action' => [ 'FormController@store' ], 'method' => 'POST']) !!}
    {!! Form::submit('test') !!}
{!! Form::close(); !!}

web.php

Route::get('/test', function ()
{
    return view('subdomains.account.pages.test');
});
Route::post('/testForm', 'FormController@store');

输出:当你使用你的路由/test时,测试按钮会显示在输出页面上

当你点击测试按钮时,它会被重定向到/testForm页面并得到输出test

【讨论】:

  • @ProGamersRo 不知道虚拟主机,但我启动php artisan serve 并在检查此代码后成功运行
  • 尝试不使用工匠服务,例如,通过转到公共文件夹打开它,例如本地主机/laravel-项目名称/公共
  • 谢谢你教我@ProGamersRo :) 我也会检查一下:http://localhost/stackoverflow/public/test 并且它工作成功,stackoverflow 是我的项目名称
  • 嗯,很奇怪,因为我不工作,这是你的 laravel 版本?,当我回家时,我会尝试重新制作项目。
  • ok cool @ProGamersRo 我有 Laravel 框架版本 5.7 ,稍后检查,如果你觉得它有用,请接受它作为正确答案。
【解决方案2】:

你应该试试这个:

{!! Form::open(['url' => 'testform.store', 'class' => 'form-horizontal', 'role' => 'form', 'method' => 'post']) !!}
    {!! Form::submit('test') !!}
{!! Form::close(); !!}

更新答案

Route::post('/testForm', 'FormController@store')->name('testform.store');

{!! Form::open(['route' => 'testform.store', 'class' => 'form-horizontal', 'role' => 'form', 'method' => 'post']) !!}
        {!! Form::submit('test') !!}
    {!! Form::close(); !!}

【讨论】:

  • 您更新的答案看起来不正确。因为,您不能使用带有 url 参数的路由名称。必须是'route' =&gt; 'testform.store'
  • 仍然无法正常工作并且您有一个错误,您无法重定向到带有url 的路由,您需要使用route
  • 现在仍在工作,但奇怪的原因是与邮递员合作。
【解决方案3】:

尝试将下面的代码用于subdomains.account.pages.test

{!! Form::open(['action' => [ '\Ares\Http\Controllers\FormController@store' ], 'method' => 'POST']) !!}
  {!! Form::submit('test') !!}
{!! Form::close(); !!}

【讨论】:

  • Symfony \ Component \ HttpKernel \ Exception \ MethodNotAllowedHttpException 表示你尝试访问的url未注册,可以尝试使用Chrome's ARCPostman访问/testForm吗?
  • 与邮递员不工作,因为我得到错误 419,邮递员只为来自 api.php 的路线工作
  • 我明白了,请试试这个question的解决方案
  • 所以邮递员工作,但从形式不工作。
  • 您是否尝试将/testFormLaravel's CRSF Protection 中排除?
【解决方案4】:
  1. 你应该远离你的路线,

    Route::get('/test', 'FromController@index');
    Route::post('/testForm', 'FormController@store');
    
  2. 然后运行,

    php artisan route:clear
    
  3. 然后提交您的表单

【讨论】:

  • 问题路由是/testForm,替换/test上的闭包应该不会影响当前问题
  • @Bondan Sebastian,他的代码没有问题。这仅仅是因为他无法执行“php artisan route:clear”。如果您的路线中有闭包,则根本无法使用路线缓存。选择性缓存会破坏对象。你应该看看这个线程。 github.com/laravel/framework/issues/7319
  • 不,我的朋友,OP 的问题是每次尝试访问 /testForm 端点时都会出现 Symfony \ Component \ HttpKernel \ Exception \ MethodNotAllowedHttpException No message 错误。
  • 那么你没有看到他所有的评论在执行“php artisan cache:clear”之后发生了什么他评论看到这张图片prnt.sc/m02oc3他发布了。
  • 我已经看到它了,从我看到的情况来看,缓存清除是成功的,只是 Laravel 无法缓存/test 端点。如果我们真的能够缓存/test 端点,testForm 仍然总是会发出错误。
猜你喜欢
  • 2018-09-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-09-20
  • 1970-01-01
相关资源
最近更新 更多