【问题标题】:Laravel - HTML POST Form, page not foundLaravel - HTML POST 表单,找不到页面
【发布时间】:2017-08-11 07:37:45
【问题描述】:

我目前正在使用 Laravel 制作这个 Web 应用程序,您可以在其中使用 HTML 表单向 MySQL 添加新产品。

问题是当我按下“添加产品”时它不起作用:

HTML 表单

<form action="store" method="POST">

                    <div class="form-group">
                        {{ Form::label('product_name', null, ['class' => 'control-label']) }}
                        {{ Form::text('product_name', null, ['class' => 'form-control']) }}
                        <br>
                    </div>
                    <div class="form-group">
                        {{ Form::label('product_description', null, ['class' => 'control-label']) }}
                        {{ Form::text('product_description', null, ['class' => 'form-control']) }}
                        <br>
                    </div>
                    <div class="form-group">
                        {{ Form::label('product_producer', null, ['class' => 'control-label']) }}
                        <select name="producator" class="form-control">
                            <option>Pick producer...</option>
                            @if(isset($marks))
                                @foreach($marks as $m)
                                    <option value="{{$m->producer_ID}}">{{$m->producer_name}}</option>
                                @endforeach
                            @endif
                        </select>
                        <br>
                    </div>
                    <button type="submit" class="btn btn-info">Add product</button>
                </form>

web.php

<?php

/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| contains the "web" middleware group. Now create something great!
|
*/

Route::get('/', function () {
    //return view('welcome');
    $products = DB::table('laravel_products')->select('product_name', 'product_about', 'producer_name', 'product_added')->get();
    $marks = DB::table('laravel_producers')->select('producer_ID', 'producer_name')->get();
    return view(
        'welcome', 
        compact('products'), 
        compact('marks')
    );
});

Route::get("index", "test@index");
Route::post("store", "test@store");

/*
Route::post('profile', 
    [
        'before' => 'csrf',
        function()
        {

        }
    ]
);*/

test.php

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Http\Requests;
use App\laravel;

class test extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        return view("welcome");
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        //
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        $user = new Laravel;
        $user->product_name = Input::get("product_name");
        $user->product_description = Input::get("product_description");
        $user->product_producer = Input::get("product_producer");
        $user->save();
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        //
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        //
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        //
    }
}

数据库表如下所示:

当我按下“添加产品”将字段中的值插入我的数据库时,我必须做什么?

提前谢谢你!

【问题讨论】:

  • 在您的控制台中,php artisan routes 是否显示 store 是有效路线?
  • @Panamera 检查我的答案,让我为你工作?
  • 如果将 Route::post 更改为 Route::get 会发生什么?
  • 同样的问题,HTML 表单发送 POST 数据并且找不到位置。因为这个问题,我想我会去吃药来治疗头痛。

标签: php html mysql laravel


【解决方案1】:

当您提交表单时,您的数据是否存储到数据库中?如果将数据存储到数据库中,那么您需要在存储数据后加载视图,在这里我可以在将数据存储到数据库后看到您没有加载视图,因为它显示您的页面未找到。谢谢

【讨论】:

  • 是的,它必须存储在数据库中。我应该怎么做?
  • @HaqNawaz 实际上,如果他不加载视图,页面应该是空白的。
  • 写出口;插入数据后的语句,它将显示一个空白页。如果你不写出口;然后它将继续执行。
【解决方案2】:

首先,您应该准备正确的路线。我的意思是你尝试通过路由'/laravel/public/store'发送发布请求来保存数据,但是你的 web.php 文件不包含这样的路由。

所以,我给你的第一个建议是正确设置根目录 (enter link description here)

【讨论】:

  • 是的:Route::post("store", "test@store")->name('store');
  • 不,这是问题所在。我看不出问题出在哪里
【解决方案3】:

当您遇到错误 NotFoundHttpException 表示路线出现问题时,请尝试以下更改:
路线

Route::post("store", "test@store")->name('store');

刀片

<form action="{{route('store')}}" method="POST">

更新
还需要在 store 方法中添加重定向:

public function store(Request $request)
{
    $user = new Laravel;
    $user->product_name = Input::get("product_name");
    $user->product_description = Input::get("product_description");
    $user->product_producer = Input::get("product_producer");
    $user->save();

    //redirect into index view
    return redirect('index')->with('status', 'Produvt Added!');;
}

【讨论】:

  • 我编辑了,但还是说:对不起,找不到您要查找的页面。
  • 知道了...让我检查一下
  • @PanameraTurboS:检查我的更新答案
  • 所以问题,没找到。我认为问题出在路线或某处。在我关注的 YouTube 视频上工作。
  • @PanameraTurboS:最后一次尝试:将此路线移动到路线文件的顶部并运行php artisan route:cache
【解决方案4】:

//添加 CSRF_Field 并将表单操作更改为 {{route('store')}}

 <form action={{route('store')}} method="POST">
    {{csrf_field()}}
                    <div class="form-group">
                        {{ Form::label('product_name', null, ['class' => 'control-label']) }}
                        {{ Form::text('product_name', null, ['class' => 'form-control']) }}
                        <br>
                    </div>
                    <div class="form-group">
                        {{ Form::label('product_description', null, ['class' => 'control-label']) }}
                        {{ Form::text('product_description', null, ['class' => 'form-control']) }}
                        <br>
                    </div>
                    <div class="form-group">
                        {{ Form::label('product_producer', null, ['class' => 'control-label']) }}
                        <select name="producator" class="form-control">
                            <option>Pick producer...</option>
                            @if(isset($marks))
                                @foreach($marks as $m)
                                    <option value="{{$m->producer_ID}}">{{$m->producer_name}}</option>
                                @endforeach
                            @endif
                        </select>
                        <br>
                    </div>
                    <button type="submit" class="btn btn-info">Add product</button>
                </form>

//控制器改为生产者而不是product_producer

public function store(Request $request)
        {
            $user = new Laravel;
            $user->product_name = Input::get("product_name");
            $user->product_description = Input::get("product_description");
            $user->product_producer = Input::get("producator");
            $user->save();
        }

//路线

Route::post("store", "test@store")->name('store');

【讨论】:

  • 实际上在 OP 问题上,异常是 NoFoundHttpException。而不是 TokenMismatchException。您可能正在解决未来的问题。但根据其他人的回答,只是将action="store" 更改为action="{{ route('store') }}" 并不能解决问题。
  • 好吧,我的错.. 我不知道这是否是问题所在,请问您是如何运行服务器的?你用过“php artisan serve”吗?如果你使用它,我认为你必须重新运行 php artisan serve,如果我之前错了,对不起..实际上我真的没有发现任何问题,它应该使用 @ 解决它987654326@@BenjaminBrasseur
  • @Vynart 我已经尝试更改 action="{{ route ('store')}}" 并且是同样的问题。但感谢您的支持。
【解决方案5】:

在路由/web.php 中 改变

Route::post("store", "test@store");

Route::post("store", "test@store")->name('store');

HTML 表单刀片 改变

<form action="store" method="POST">

<form action="{{route('store')}}" method="POST">

【讨论】:

    【解决方案6】:

    &lt;form method='post' action=''&gt; 下方添加@csrf 令牌。希望能解决问题。

    【讨论】:

    • 具体添加到哪里?
    【解决方案7】:

    您必须首先创建一条路线,如前所述。在您的网络路由中添加行

    Route::post("store", "test@store")->name('store');
    

    并将您的表单发布操作更改为

    {{route('store')}}
    

    希望对你有用。

    【讨论】:

    • 已经完成,页面显示。但是当我按下“添加产品”时仍然显示 (1/1) NotFoundHttpException
    猜你喜欢
    • 2012-04-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-08-24
    • 2021-07-29
    • 2013-08-17
    • 2015-12-21
    相关资源
    最近更新 更多