在 Laravel 的 POST 表单上发送信息时出错

Posted

技术标签:

【中文标题】在 Laravel 的 POST 表单上发送信息时出错【英文标题】:Error sending information on POST form in Laravel 【发布时间】:2020-02-16 12:45:34 【问题描述】:

我是 Laravel,显示错误“此路由不支持 POST 方法。支持的方法:GET、HEAD、PUT、PATCH、DELETE。”我正在使用表单 :: open 并使用以下代码:

查看注册:

视图的想法是用更友好的方式向用户展示如何注册使用系统

@section('content')


<div class="card card-register mx-auto mt-5">
    <div class="card-header">Criar Conta</div>
    <div class="card-body">
        <div class="justify-content-center">
            Form::model(['route' => 'users.store', 'method' => 'post'])
            <div class="form-row">
                <div class="col-md-6">
                    Form::label('nome', 'Nome:')
                    Form::text('nome',null,['class' => 'form-control'])
                    @if ($errors->has('nome'))
                    $errors->first('nome')
                    @endif
                </div>
            </div>



            <div class="form-row">
                <div class="col-md-6">
                    Form::label('sobrenome', 'Sobrenome:')
                    Form::text('sobrenome',null, ['class' => 'form-control'])
                    @if ($errors->has('nome'))
                    $errors->first('nome')
                    @endif
                </div>
            </div>




            <div class="form-row">
                <div class="col-md-6">
                    Form::label('datanascimento', 'Data de Nascimento:')
                    Form::date('datanascimento',null,['class' => 'form-control'])
                    @if ($errors->has('datanascimento'))
                    $errors->first('datanascimento')
                    @endif
                </div>
            </div>

            <div class="form-row">
                <div class="col-md-6">
                    Form::label('cpf', 'CPF:')
                    Form::text('cpf',null,['class' => 'form-control'])
                    @if ($errors->has('cpf'))
                    $errors->first('cpf')
                    @endif
                </div>
            </div>

            <div class="form-row">
                <div class="col-md-6">
                    Form::label('rg', 'RG:')
                    Form::text('rg',null,['class' => 'form-control'])
                    @if ($errors->has('rg'))
                    $errors->first('rg')
                    @endif
                </div>
            </div>


            <div class="form-row">
                <div class="col-md-6">
                    Form::label('telefone', 'Telefone:')
                    Form::text('telefone',null,['class' => 'form-control'])
                    @if ($errors->has('telefone'))
                    $errors->first('telefone')
                    @endif
                </div>
            </div>

            <div class="form-row">
                <div class="col-md-6">
                    Form::label('celular', 'Celular:')
                    Form::text('celular',null,['class' => 'form-control'])
                    @if ($errors->has('celular'))
                    $errors->first('celular')
                    @endif
                </div>
            </div>

            <div class="form-row">
                <div class="col-md-6">
                    Form::label('rua', 'Rua:')
                    Form::text('rua',null,['class' => 'form-control'])
                    @if ($errors->has('rua'))
                    $errors->first('rua')
                    @endif
                </div>
            </div>

            <div class="form-row">
                <div class="col-md-6">
                    Form::label('bairro', 'Bairro:')
                    Form::text('bairro',null,['class' => 'form-control'])
                    @if ($errors->has('bairro'))
                    $errors->first('bairro')
                    @endif
                </div>
            </div>


            <div class="form-row">
                <div class="col-md-6">
                    Form::label('complemento', 'Complemento:')
                    Form::text('complemento',null,['class' => 'form-control'])
                    @if ($errors->has('complemento'))
                    $errors->first('complemento')
                    @endif
                </div>
            </div>

            <div class="form-row">
                <div class="col-md-6">
                    Form::label('numero', 'Número:')
                    Form::text('numero',null,['class' => 'form-control'])
                    @if ($errors->has('numero'))
                    $errors->first('numero')
                    @endif
                </div>
            </div>

            <div class="form-row">
                <div class="col-md-6">
                    Form::label('cep', 'CEP:')
                    Form::text('cep',null,['class' => 'form-control'])
                    @if ($errors->has('cep'))
                    $errors->first('cep')
                    @endif
                </div>
            </div>

            <div class="form-row">
                <div class="col-md-6">
                    Form::label('email', 'Email:')
                    Form::text('email',null,['class' =>'form-control'])
                    @if ($errors->has('email'))
                    $errors->first('email')
                    @endif
                </div>
            </div>

            <div class="form-row">
                <div class="col-md-6">
                    Form::label('password', 'Senha:')
                    Form::password('password',['class' => 'form-control', 'type' => 'password'])
                    @if ($errors->has('password'))
                    $errors->first('password')
                    @endif
                </div>
            </div>

            <div class="form-row">
                <div class="col-md-6">
                    Form::label('password_confirmation', 'Confirmação Senha:')
                    Form::password('password_confirmation',['class' => 'form-control'])
                    @if ($errors->has('password_confirmation'))
                    $errors->first('password_confirmation')
                    @endif
                </div>
            </div>

            <div class="mt-3">
                Form::submit('Salvar',array('class'=> 'btn btn-primary', 'id' => 'validar'))
                Form::reset('Limpar', array('class' => 'btn btn-danger'))
            </div>
            Form::close()

        </div>
    </div>
</div>

@endsection

这是 laravel 路由文件:

Auth::routes();

Route::group(['middleware' => ['web', 'auth']], function () 
    Route::get('/', function () 
        if (Auth::user()->role == 0) 
            return view('home');
         elseif (Auth::user()->role == 1) 
            return view('homeemployee');
         elseif (Auth::user()->role == 2) 
            return view('homerequest');
        
    );
);

Route::resource('users', 'UserController');

Este é o UserController:

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\User;

class UserController extends Controller

    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    
        $users = User::all();
        return view('users.index', compact('users'));
    


    public function create()
    
        $users = new User();
        return view('users.create')->with('usuario', $users);
    

    public function edit($id)
    
        $users = User::find($id);
        return view('users.edit')->with('usuario', $users); 
    

    public function destroy(Request $request, $id)
    
        $users = User::find($id);
        $users->delete();

        $request->session()->flash('message', 'Excluido com sucesso');
        return redirect('users');
    

    public function store(Request $request)
           
        $users = new User();
        $users->nome = $request->input('nome');
        $users->sobrenome = $request->input('sobrenome');
        $users->email= $request->input('email');
        $users->password = Hash::make($request->input('password'));
        $users->cpf = $request->input('cpf');
        $users->rg = $request->input('rg');
        $users->telefone= $request->input('telefone');
        $users->celular= $request->input('celular');
        $users->rua= $request->input('rua');
        $users->bairro= $request->input('bairro');
        $users->numero= $request->input('numero');
        $users->complemento= $request->input('complemento');
        $users->cep= $request->input('cep');
        $users->role=2;
        $users->datanascimento = $request->input('datanascimento');

        if ($users->save()) 
            $request->session()->flash('message', 'Usuário salvo com sucesso.');
         else 
            $request->session()->flash('message', 'Ocorreu um erro ao cadastrar.');
        

        return redirect()->route('users.index');
    

    public function update(Request $request, $id)
    

        $request->validate([
            'nome' => 'required'
        ]);
        $user = User::find($id);
        $user->nome = $request->input('nome');
        $user->sobrenome = $request->input('sobrenome');
        $user->email= $request->input('email');
        $user->password = Hash::make($request->input('password'));
        $user->cpf = $request->input('cpf');
        $user->rg = $request->input('rg');
        $user->telefone= $request->input('telefone');
        $user->celular= $request->input('celular');
        $user->rua= $request->input('rua');
        $user->bairro= $request->input('bairro');
        $user->numero= $request->input('numero');
        $user->complemento= $request->input('complemento');
        $user->cep= $request->input('cep');
        $user->role=2;
        $user->datanascimento = $request->input('datanascimento');
        $user->save();

        $request->session()->flash('message', 'Atualizado com sucesso');

        return redirect('users');
    

Error Image

【问题讨论】:

【参考方案1】:

您似乎没有在中间件组中定义路由。所以这一行:

Form::model(['route' => 'users.store', 'method' => 'post'])

正在通过POST 方法发回您的表单并寻找名为users.store 的命名路由。但是,您发布的路由文件未定义 users.store 路由。我希望在您的路线中看到类似的内容:

 Route::post('users', 'UserController@store')->name('users.store');

这将清除错误,因为您已在 web.php 中命名了路由。

话说,把你的自定义user资源放在上面的中间件块里面。

IE:

Route::group(['middleware' => ['web', 'auth']], function () 
    Route::resource('users', 'UserController'); // etc.

【讨论】:

我将路由定义为:Route::resource('users', 'UserController'); 看起来不错。您是否在您的 web / auth 中间件组中尝试过? 这很奇怪。我没有看到您的代码有任何其他问题。它可能与其他事物发生冲突。如果你把Auth::routes();web.php中去掉再试试,是不是还有这个错误? 如果我删除 Auth :: routes (); 我会丢失 login 路由,因为我使用了 Laravel Auth。 是的,我明白了。我们只是想测试看看是否有东西与路线冲突。您可以尝试重命名资源而不是删除身份验证吗?也许Route::resource('customUsers', 'UserController'); 然后在你的表单上匹配这个来测试看看是不是名字的问题。

以上是关于在 Laravel 的 POST 表单上发送信息时出错的主要内容,如果未能解决你的问题,请参考以下文章

Laravel用post提交表单常见的两个错误

Laravel:通过无表单的 AJAX 向控制器发送数据

在 Laravel 8 中使用 API 路由时返回错误

表单提交后,在 laravel 中向用户显示提交的数据

发送无表单的 POST 数据

使用 php 和 laravel 显示表单数据