如何在Laravel中使用get route插入数据发布

问题描述 投票:0回答:1

我如何在Laravel中使用POST路由发送表单GET方法?

路线

Route::get('domain_detail/{domain_name}','domain_detailController@index');

View domain_detail文件夹

<form method="post" action="{{url('domain_detail')}}/{{strtolower($domain_detail->domain_name)}}">
    <div class="form-group">
        <label for="namefamily">namefamily</label>
        <input type="text" class="form-control round shadow-sm bg-white text-dark" name="namefamily">
    </div>
    <div class="form-group">
        <label for="mobile">mobile</label>
        <input type="text" class="form-control round shadow-sm bg-white text-dark" name="mobile">
    </div>
    <div class="form-group">
        <label for="myprice">myprice</label>
        <input type="number" class="form-control round shadow-sm bg-white text-dark" name="myprice">
    </div>
    <div class="form-group">
        <input type="submit" name="send_price" class="btn btn-success" value="submit">
    </div>
</form>

Controller

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;


class domain_detailController extends Controller
{
    public function index($domain_name)
    {
        $domain_detail_exist = DB::table("domains")->where('domain_name', $domain_name)->exists();
        if ($domain_detail_exist) {
            $domain_detail = DB::table("domains")->where('domain_name', $domain_name)->first();

            return view('domain_detail/index', ['domain_detail' => $domain_detail]);
        } else {
            return view('404');
        }
    }

    public function create()
    {
        return view('domain_detail.index');
    }
}

在控制器上,我没有在create函数中输入任何代码,但是当我单击表单中的提交按钮时,出现此错误

Symfony \ Component \ HttpKernel \ Exception \ MethodNotAllowedHttpException该路由不支持POST方法。支持的方法:GET,HEAD。

php laravel
1个回答
2
投票

在您的domain_detailController中使用索引函数,以便返回视图。像这样:

public function index($domain_name)
{
   return view('domain_detail.index'); 
}

创建返回视图的路线:

Route::get('domain_detail/','domain_detailController@index');

然后使用create函数像这样存储域详细信息:

public function create($domain_name)
    {
        $domain_detail_exist = DB::table("domains")->where('domain_name', $domain_name)->exists();
        if ($domain_detail_exist) {
            $domain_detail = DB::table("domains")->where('domain_name', $domain_name)->first();

            return view('domain_detail/index', ['domain_detail' => $domain_detail]);
        } else {
            return view('404');
        }
    }

进行这样的POST路由:

Route::post('domain_detail/','domain_detailController@create');

也请参考有关命名约定的laravel最佳实践:https://www.laravelbestpractices.com/

© www.soinside.com 2019 - 2024. All rights reserved.