【问题标题】:how can insert data post with get route in Laravel如何在 Laravel 中使用 get route 插入数据发布
【发布时间】:2020-08-08 00:00:30
【问题描述】:

如何在 laravel 中发送带有GET 路由的表单POST 方法?

路线

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

查看 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>

控制器

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 方法。支持的方法: 得到,头。

【问题讨论】:

  • 尝试将表单的方法设置为获取而不是发布:
  • @Yasin Karimian,我的遮阳篷解决了您的问题吗?如果是这样,请将其标记为正确,以便可能遇到相同问题的其他人可以查阅此帖子以找到可能的解决方案

标签: php laravel


【解决方案1】:

在你的 domain_detailController 中使用 index 函数来返回视图。 像这样:

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/

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-02-07
    • 2018-02-01
    • 1970-01-01
    • 2020-03-12
    • 2023-03-14
    • 1970-01-01
    • 2023-04-04
    相关资源
    最近更新 更多