关于php:如何在Laravel中使用get route插入数据发布

how can insert data post with get route in Laravel

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

路线

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

查看domain_detail文件夹

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
<form method="post" action="{{url('domain_detail')}}/{{strtolower($domain_detail->domain_name)}}">
   
        <label for="namefamily">namefamily</label>
        <input type="text" class="form-control round shadow-sm bg-white text-dark" name="namefamily">
   
   
        <label for="mobile">mobile</label>
        <input type="text" class="form-control round shadow-sm bg-white text-dark" name="mobile">
   
   
        <label for="myprice">myprice</label>
        <input type="number" class="form-control round shadow-sm bg-white text-dark" name="myprice">
   
   
        <input type="submit" name="send_price" class="btn btn-success" value="submit">
   
</form>

控制器

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
namespace App\\Http\\Controllers;

use Illuminate\\Http\
equest;
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
The POST method is not supported for this route. Supported methods:
GET, HEAD.


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

1
2
3
4
public function index($domain_name)
{
   return view('domain_detail.index');
}

创建返回视图的路线:

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

然后使用create函数存储域详细信息,如下所示:

1
2
3
4
5
6
7
8
9
10
11
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路由:

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

在命名约定方面,还请查看laravel最佳实践:
https://www.laravelbestpractices.com/